| 713 | }) |
| 714 | |
| 715 | function createServer (opts) { |
| 716 | var _bodyParser = typeof opts !== 'function' |
| 717 | ? bodyParser.json(opts) |
| 718 | : opts |
| 719 | |
| 720 | return http.createServer(function (req, res) { |
| 721 | _bodyParser(req, res, function (err) { |
| 722 | if (err) { |
| 723 | res.statusCode = err.status || 500 |
| 724 | res.end(req.headers['x-error-property'] |
| 725 | ? err[req.headers['x-error-property']] |
| 726 | : ('[' + err.type + '] ' + err.message)) |
| 727 | } else { |
| 728 | res.statusCode = 200 |
| 729 | res.end(JSON.stringify(req.body)) |
| 730 | } |
| 731 | }) |
| 732 | }) |
| 733 | } |
| 734 | |
| 735 | function parseError (str) { |
| 736 | try { |