jsonBodyParser.js
808 Bytes
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
// Generated by CoffeeScript 1.8.0
var jsonBodyParser, parseBodyAsJSON;
jsonBodyParser = function() {
return function(request, response, next) {
return parseBodyAsJSON(request, response, next);
};
};
parseBodyAsJSON = function(request, response, next) {
var buffer;
if (request.body) {
return next();
}
request.body = {};
if (request.method !== 'POST') {
return next();
}
request.setEncoding('utf8');
buffer = '';
request.on('data', function(chunk) {
return buffer += chunk;
});
return request.on('end', function() {
var e;
if ('' === buffer) {
return next();
}
try {
request.body = JSON.parse(buffer);
return next();
} catch (_error) {
e = _error;
return next(e);
}
});
};
module.exports = jsonBodyParser;