index.js
function finalhandler (req, res, options) {
  var opts = options || {}

  // get environment
  var env = opts.env || process.env.NODE_ENV || 'development'

  // get error callback
  var onerror = opts.onerror

  return function (err) {
    var headers
    var msg
    var status

    // ignore 404 on in-flight response
    if (!err && headersSent(res)) {
      debug('cannot 404 after headers sent')
      return
    }

    // unhandled error
    if (err) {
      // respect status code from error
      status = getErrorStatusCode(err)

      if (status === undefined) {
        // fallback to status code on response
        status = getResponseStatusCode(res)
      } else {
        // respect headers from error
        headers = getErrorHeaders(err)
      }

      // get error message
      msg = getErrorMessage(err, status, env)
    } else {
      // not found
      status = 404
      msg = 'Cannot ' + req.method + ' ' + encodeUrl(getResourceName(req))
    }

    debug('default %s', status)

    // schedule onerror callback
    if (err && onerror) {
      defer(onerror, err, req, res)
    }

    // cannot actually respond
    if (headersSent(res)) {
      debug('cannot %d after headers sent', status)
      req.socket.destroy()
      return
    }

    // send response
    send(req, res, status, headers, msg)
  }
}
function finalhandler (req, res, options) {
  var opts = options || {}

  // get environment
  var env = opts.env || process.env.NODE_ENV || 'development'

  // get error callback
  var onerror = opts.onerror

  return function (err) {
    var headers
    var msg
    var status

    // ignore 404 on in-flight response
    if (!err && headersSent(res)) {
      debug('cannot 404 after headers sent')
      return
    }

    // unhandled error
    if (err) {
      // respect status code from error
      status = getErrorStatusCode(err)

      if (status === undefined) {
        // fallback to status code on response
        status = getResponseStatusCode(res)
      } else {
        // respect headers from error
        headers = getErrorHeaders(err)
      }

      // get error message
      msg = getErrorMessage(err, status, env)
    } else {
      // not found
      status = 404
      msg = 'Cannot ' + req.method + ' ' + encodeUrl(getResourceName(req))
    }

    debug('default %s', status)

    // schedule onerror callback
    if (err && onerror) {
      defer(onerror, err, req, res)
    }

    // cannot actually respond
    if (headersSent(res)) {
      debug('cannot %d after headers sent', status)
      req.socket.destroy()
      return
    }

    // send response
    send(req, res, status, headers, msg)
  }
}
function finalhandler ( req , res , options ) { var opts = options || { } // get environment var env = opts . env || process . env . NODE_ENV || 'development' // get error callback var onerror = opts . onerror return function ( err ) { var headers var msg var status // ignore 404 on in-flight response if ( ! err && headersSent ( res ) ) { debug ( 'cannot 404 after headers sent' ) return } // unhandled error if ( err ) { // respect status code from error status = getErrorStatusCode ( err ) if ( status === undefined ) { // fallback to status code on response status = getResponseStatusCode ( res ) } else { // respect headers from error headers = getErrorHeaders ( err ) } // get error message msg = getErrorMessage ( err , status , env ) } else { // not found status = 404 msg = 'Cannot ' + req . method + ' ' + encodeUrl ( getResourceName ( req ) ) } debug ( 'default %s' , status ) // schedule onerror callback if ( err && onerror ) { defer ( onerror , err , req , res ) } // cannot actually respond if ( headersSent ( res ) ) { debug ( 'cannot %d after headers sent' , status ) req . socket . destroy ( ) return } // send response send ( req , res , status , headers , msg ) } }
Create a function to handle the final response.

@param {Request} req
@param {Response} res
@param {Object} [options]
@return {Function}
@public
Create a function to handle the final response .