modules/orionode/lib/api.js
function writeResponse(code, res, headers, body, needEncodeLocation, noCachedStringRes) {
	try{
		if (res.headerSent) {
			logger.error("header Sent:", res.req.method, res.req.originalUrl);
		}
		if (typeof code === 'number') {
			if (httpCodeMapping) {
				code = mapHttpStatusCode(code);
			}
			res.status(code);
		}
		if (headers && typeof headers === 'object') {
			Object.keys(headers).forEach(function(header) {
				var value = headers[header];
				res.setHeader(
					header.replace(REGEX_CRLF, ""),
					typeof value === "string" ? value.replace(REGEX_CRLF, "") : value
				);
			});
		}
		addStrictTransportHeaders(res);
		if (typeof body !== 'undefined') {
			if (typeof body === 'object') {
				needEncodeLocation && encodeLocation(body);
				setResponseNoCache(res);			
				return res.json(body);
			}
			if (noCachedStringRes){
				setResponseNoCache(res);
			}
			res.send(body);
		} else {
			setResponseNoCache(res);
			res.end();
		}
	} catch(err) {
		logger.error(res.req.originalUrl, err.message);
		throw err;
	}
}
function writeResponse(code, res, headers, body, needEncodeLocation, noCachedStringRes) {
	try{
		if (res.headerSent) {
			logger.error("header Sent:", res.req.method, res.req.originalUrl);
		}
		if (typeof code === 'number') {
			if (httpCodeMapping) {
				code = mapHttpStatusCode(code);
			}
			res.status(code);
		}
		if (headers && typeof headers === 'object') {
			Object.keys(headers).forEach(function(header) {
				var value = headers[header];
				res.setHeader(
					header.replace(REGEX_CRLF, ""),
					typeof value === "string" ? value.replace(REGEX_CRLF, "") : value
				);
			});
		}
		addStrictTransportHeaders(res);
		if (typeof body !== 'undefined') {
			if (typeof body === 'object') {
				needEncodeLocation && encodeLocation(body);
				setResponseNoCache(res);			
				return res.json(body);
			}
			if (noCachedStringRes){
				setResponseNoCache(res);
			}
			res.send(body);
		} else {
			setResponseNoCache(res);
			res.end();
		}
	} catch(err) {
		logger.error(res.req.originalUrl, err.message);
		throw err;
	}
}
function writeResponse ( code , res , headers , body , needEncodeLocation , noCachedStringRes ) { try { if ( res . headerSent ) { logger . error ( "header Sent:" , res . req . method , res . req . originalUrl ) ; } if ( typeof code === 'number' ) { if ( httpCodeMapping ) { code = mapHttpStatusCode ( code ) ; } res . status ( code ) ; } if ( headers && typeof headers === 'object' ) { Object . keys ( headers ) . forEach ( function ( header ) { var value = headers [ header ] ; res . setHeader ( header . replace ( REGEX_CRLF , "" ) , typeof value === "string" ? value . replace ( REGEX_CRLF , "" ) : value ) ; } ) ; } addStrictTransportHeaders ( res ) ; if ( typeof body !== 'undefined' ) { if ( typeof body === 'object' ) { needEncodeLocation && encodeLocation ( body ) ; setResponseNoCache ( res ) ; return res . json ( body ) ; } if ( noCachedStringRes ) { setResponseNoCache ( res ) ; } res . send ( body ) ; } else { setResponseNoCache ( res ) ; res . end ( ) ; } } catch ( err ) { logger . error ( res . req . originalUrl , err . message ) ; throw err ; } }
Helper for writing a JSON response.
@param {Number} code
@param {HttpResponse} res
@param {Object} [headers]
@param {Object|String} [body] If Object, response will be JSON. If string, response will be text/plain.
@param {Boolean} needEncodeLocation
@param {Boolean} noCachedStringRes,set to true in case if the response is text/plain, but still need nocache cache-control
Helper for writing a JSON response .