-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (34 loc) · 955 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
var url = require('url');
var qs = require('querystring');
function Parse (func) {
return function(req, res){
// Handle GET variables
var parts = url.parse(req.url,true);
if (parts.query) req.get = parts.query;
// Handle POST variables
if (req.method === 'POST') {
req.postData = "";
req.on('data', function(data){
req.postData += data;
// Check data size
if (req.postData.length > 1e7) { // 10MB limit
req.postData = '';
response.writeHead(413, {'Content-Type': 'text/plain'}).end();
request.connection.destroy();
}
});
req.on('end', function(){
// If not posted as binary data
if (req.headers['content-type'].toLowerCase() !== 'multipart/form-data' ) {
req.post = qs.parse(req.postData);
func(req, res);
}else{
// TODO add binary support... Possiblly with poor-form
}
});
}else{
func(req, res);
}
};
};
module.exports = Parse;