relu-core
Version:
41 lines (33 loc) • 950 B
JavaScript
// Copyright 2012 Mark Cavage, Inc. All rights reserved.
;
///--- Exports
/**
* JSON formatter.
* @public
* @function formatJSON
* @param {Object} req the request object
* @param {Object} res the response object
* @param {Object} body response body
* @param {Function} cb cb
* @returns {String}
*/
function formatJSON(req, res, body, cb) {
if (body instanceof Error) {
// snoop for RestError or HttpError, but don't rely on
// instanceof
res.statusCode = body.statusCode || 500;
if (body.body) {
body = body.body;
} else {
body = {
message: body.message
};
}
} else if (Buffer.isBuffer(body)) {
body = body.toString('base64');
}
var data = JSON.stringify(body);
res.setHeader('Content-Length', Buffer.byteLength(data));
return cb(null, data);
}
module.exports = formatJSON;