-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
39 lines (32 loc) · 945 Bytes
/
server.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
var concat = require('concat-stream');
var Response = require('./response');
module.exports = function(opts) {
opts = opts || {};
function debug(s) {
if (opts.verbose || (process.env.VERBOSE && process.env.VERBOSE != "")) {
console.log(s);
}
}
return function(req, res) {
req.pipe(concat(function(body) {
if (!body) {
res.statusCode = 500;
res.end('No POST body found');
return;
}
// Parse the response body
body = JSON.parse(body.toString());
var response = Response(opts.root, opts.verbose, body);
// No response, error
if (!response) {
console.log('Error: No Type found in body');
res.statusCode = 500;
res.end('No Type found in body');
return;
};
// Finish up the processing.
res.setHeader('Content-type', 'application/json');
res.end(JSON.stringify(response, null, 4));
}));
}
}