forked from binocarlos/powerstrip-debug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
40 lines (33 loc) · 1016 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
40
var concat = require('concat-stream')
var Logger = require('./logger')
var Response = require('./response')
module.exports = function(opts){
opts = opts || {}
return function(req, res){
req.pipe(concat(function(body){
if(!body){
res.statusCode = 500;
res.end('No POST body found')
return
}
body = JSON.parse(body.toString())
var response = Response(body)
var output = Logger(body)
if(opts.verbose){
output += "\n\n";
output += "-------------------------------------------\n"
output += JSON.stringify(body, null, 4);
output += "\n-------------------------------------------\n";
}
if(!response){
console.log('Error: No Type found in body')
res.statusCode = 500;
res.end('No Type found in body')
return
}
console.log(output)
res.setHeader('Content-type', 'application/json')
res.end(JSON.stringify(response, null, 4))
}))
}
}