bin/server/sans-server.js
function transform(req, res, next) {
    const state = res.state;
    const body = state.body;
    const type = typeof body;
    const isBuffer = body instanceof Buffer;
    let contentType;

    // error conversion
    if (body instanceof Error) {
        res.log('transform', 'Converting Error to response');
        res.status(500).body(httpStatus[500]).set('content-type', 'text/plain');

    // buffer conversion
    } else if (isBuffer) {
        res.log('transform', 'Converting Buffer to base64 string');
        res.body(body.toString('base64'));
        contentType = 'application/octet-stream';

    // object conversion
    } else if (type === 'object') {
        res.log('transform', 'Converting object to JSON string');
        res.body(JSON.stringify(body));
        contentType = 'application/json';

    // not string conversion
    } else if (type !== 'string') {
        res.log('transform', 'Converting ' + type + ' to string');
        res.body(String(body));
        contentType = 'text/plain';

    } else {
        contentType = 'text/html';
    }

    // set content type if not yet set
    if (!state.headers.hasOwnProperty('content-type') && contentType) {
        res.log('transform', 'Set content type');
        res.set('Content-Type', contentType);
    }

    next();
}
function transform(req, res, next) {
    const state = res.state;
    const body = state.body;
    const type = typeof body;
    const isBuffer = body instanceof Buffer;
    let contentType;

    // error conversion
    if (body instanceof Error) {
        res.log('transform', 'Converting Error to response');
        res.status(500).body(httpStatus[500]).set('content-type', 'text/plain');

    // buffer conversion
    } else if (isBuffer) {
        res.log('transform', 'Converting Buffer to base64 string');
        res.body(body.toString('base64'));
        contentType = 'application/octet-stream';

    // object conversion
    } else if (type === 'object') {
        res.log('transform', 'Converting object to JSON string');
        res.body(JSON.stringify(body));
        contentType = 'application/json';

    // not string conversion
    } else if (type !== 'string') {
        res.log('transform', 'Converting ' + type + ' to string');
        res.body(String(body));
        contentType = 'text/plain';

    } else {
        contentType = 'text/html';
    }

    // set content type if not yet set
    if (!state.headers.hasOwnProperty('content-type') && contentType) {
        res.log('transform', 'Set content type');
        res.set('Content-Type', contentType);
    }

    next();
}
function transform ( req , res , next ) { const state = res . state ; const body = state . body ; const type = typeof body ; const isBuffer = body instanceof Buffer ; let contentType ; // error conversion if ( body instanceof Error ) { res . log ( 'transform' , 'Converting Error to response' ) ; res . status ( 500 ) . body ( httpStatus [ 500 ] ) . set ( 'content-type' , 'text/plain' ) ; // buffer conversion } else if ( isBuffer ) { res . log ( 'transform' , 'Converting Buffer to base64 string' ) ; res . body ( body . toString ( 'base64' ) ) ; contentType = 'application/octet-stream' ; // object conversion } else if ( type === 'object' ) { res . log ( 'transform' , 'Converting object to JSON string' ) ; res . body ( JSON . stringify ( body ) ) ; contentType = 'application/json' ; // not string conversion } else if ( type !== 'string' ) { res . log ( 'transform' , 'Converting ' + type + ' to string' ) ; res . body ( String ( body ) ) ; contentType = 'text/plain' ; } else { contentType = 'text/html' ; } // set content type if not yet set if ( ! state . headers . hasOwnProperty ( 'content-type' ) && contentType ) { res . log ( 'transform' , 'Set content type' ) ; res . set ( 'Content-Type' , contentType ) ; } next ( ) ; }
Response middleware for transforming the response body and setting content type.
@param {Request} req
@param {Response} res
@param {function} next
Response middleware for transforming the response body and setting content type .