| 501 | }) |
| 502 | |
| 503 | function createServer (opts) { |
| 504 | const _bodyParser = typeof opts !== 'function' |
| 505 | ? bodyParser.raw(opts) |
| 506 | : opts |
| 507 | |
| 508 | return http.createServer(function (req, res) { |
| 509 | _bodyParser(req, res, function (err) { |
| 510 | if (err) { |
| 511 | res.statusCode = err.status || 500 |
| 512 | res.end('[' + err.type + '] ' + err.message) |
| 513 | return |
| 514 | } |
| 515 | |
| 516 | if (Buffer.isBuffer(req.body)) { |
| 517 | res.end('buf:' + req.body.toString('hex')) |
| 518 | return |
| 519 | } |
| 520 | |
| 521 | res.end(JSON.stringify(req.body) || typeof req.body) |
| 522 | }) |
| 523 | }) |
| 524 | } |