Name: js-handler/node_modules/restify/lib/formatters/json.js
| 1: | // Copyright 2012 Mark Cavage, Inc. All rights reserved. |
| 2: | |
| 3: | |
| 4: | |
| 5: | ///--- Exports |
| 6: | |
| 7: | function formatJSON(req, res, body) { |
| 8: | if (body instanceof Error) { |
| 9: | // snoop for RestError or HttpError, but don't rely on |
| 10: | // instanceof |
| 11: | res.statusCode = body.statusCode || 500; |
| 12: | |
| 13: | if (body.body) { |
| 14: | body = body.body; |
| 15: | } else { |
| 16: | body = { |
| 17: | message: body.message |
| 18: | }; |
| 19: | } |
| 20: | } else if (Buffer.isBuffer(body)) { |
| 21: | body = body.toString('base64'); |
| 22: | } |
| 23: | |
| 24: | var data = JSON.stringify(body); |
| 25: | res.setHeader('Content-Length', Buffer.byteLength(data)); |
| 26: | |
| 27: | return (data); |
| 28: | } |
| 29: | |
| 30: | module.exports = formatJSON; |
