| 965 | }) |
| 966 | |
| 967 | function createServer (opts) { |
| 968 | const _bodyParser = typeof opts !== 'function' |
| 969 | ? bodyParser.json(opts) |
| 970 | : opts |
| 971 | |
| 972 | return http.createServer(function (req, res) { |
| 973 | _bodyParser(req, res, function (err) { |
| 974 | if (err) { |
| 975 | res.statusCode = err.status || 500 |
| 976 | res.end(req.headers['x-error-property'] |
| 977 | ? err[req.headers['x-error-property']] |
| 978 | : ('[' + err.type + '] ' + err.message)) |
| 979 | } else { |
| 980 | res.statusCode = 200 |
| 981 | res.end(JSON.stringify(req.body) || typeof req.body) |
| 982 | } |
| 983 | }) |
| 984 | }) |
| 985 | } |
| 986 | |
| 987 | function parseError (str) { |
| 988 | try { |