controller.js
function(req, res, next) {
    // If we timed out before managing to respond, don't send the response
    if (res.headersSent) {
      return;
    }

    // Look for `.json` or `.xml` extension in path
    // And override request accept header
    if (/.json$/.test(req.path)) {
      req.headers.accept = 'application/json';
    } else if (/.xml$/.test(req.path)) {
      req.headers.accept = 'application/xml';
    }

    // Use request accept header to determine response content-type
    res.format({
      json: function() {
        res.status(res.code).jsonp(res.data);
      },
      xml: function() {
        var xml;
        try {
          var xmlData = JSON.parse(JSON.stringify(res.data));
          xml = this.xmlBuilder.buildObject(xmlData);
          res.set('Content-Type', 'application/xml; charset=utf-8');
          res.status(res.code).send(xml);
        } catch (e) {
          res.status(500).end();
        }
      }.bind(this)
    });
  }
function(req, res, next) {
    // If we timed out before managing to respond, don't send the response
    if (res.headersSent) {
      return;
    }

    // Look for `.json` or `.xml` extension in path
    // And override request accept header
    if (/.json$/.test(req.path)) {
      req.headers.accept = 'application/json';
    } else if (/.xml$/.test(req.path)) {
      req.headers.accept = 'application/xml';
    }

    // Use request accept header to determine response content-type
    res.format({
      json: function() {
        res.status(res.code).jsonp(res.data);
      },
      xml: function() {
        var xml;
        try {
          var xmlData = JSON.parse(JSON.stringify(res.data));
          xml = this.xmlBuilder.buildObject(xmlData);
          res.set('Content-Type', 'application/xml; charset=utf-8');
          res.status(res.code).send(xml);
        } catch (e) {
          res.status(500).end();
        }
      }.bind(this)
    });
  }
function ( req , res , next ) { // If we timed out before managing to respond, don't send the response if ( res . headersSent ) { return ; } // Look for `.json` or `.xml` extension in path // And override request accept header if ( / .json$ / . test ( req . path ) ) { req . headers . accept = 'application/json' ; } else if ( / .xml$ / . test ( req . path ) ) { req . headers . accept = 'application/xml' ; } // Use request accept header to determine response content-type res . format ( { json : function ( ) { res . status ( res . code ) . jsonp ( res . data ) ; } , xml : function ( ) { var xml ; try { var xmlData = JSON . parse ( JSON . stringify ( res . data ) ) ; xml = this . xmlBuilder . buildObject ( xmlData ) ; res . set ( 'Content-Type' , 'application/xml; charset=utf-8' ) ; res . status ( res . code ) . send ( xml ) ; } catch ( e ) { res . status ( 500 ) . end ( ) ; } } . bind ( this ) } ) ; }
Final middleware for handling all responses
Server actually responds and terminates to the request here
Final middleware for handling all responses Server actually responds and terminates to the request here