Skip to content
Myr

use finalhandler for final response handling

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Douglas Christopher Wilson · committed by Douglas Christopher Wilson · parent 3ffceff · Browse files · 3 files+20 added−39 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+20 added−39 removed

  • other:+20 added−39 removed

3 files+20 added−39 removed

modified:lib/application.js+18 added-39 removed
1/**2 * Module dependencies.3 */45var finalhandler = require('finalhandler');6var mixin = require('utils-merge');6var escapeHtml = require('escape-html');7var Router = require('./router');8var methods = require('methods');9var middleware = require('./middleware/init');10var query = require('./middleware/query');
107 unmodified lines
118 * @api private119 */120121app.handle = function(req, res, done) {122 var env = this.get('env');123124 this._router.handle(req, res, function(err) {125 if (done) {126 return done(err);127 }128129 // unhandled error130 if (err) {131 // default to 500132 if (res.statusCode < 400) res.statusCode = 500;133 debug('default %s', res.statusCode);134135 // respect err.status136 if (err.status) res.statusCode = err.status;137138 // production gets a basic error message139 var msg = 'production' == env140 ? http.STATUS_CODES[res.statusCode]141 : err.stack || err.toString();142 msg = escapeHtml(msg);143144 // log to stderr in a non-test env145 if ('test' != env) console.error(err.stack || err.toString());146 if (res.headersSent) return req.socket.destroy();147 res.setHeader('Content-Type', 'text/html');148 res.setHeader('Content-Length', Buffer.byteLength(msg));149 if ('HEAD' == req.method) return res.end();150 res.end(msg);151 return;152 }153154 // 404155 debug('default 404');156 res.statusCode = 404;157 res.setHeader('Content-Type', 'text/html');158 if ('HEAD' == req.method) return res.end();159 res.end('Cannot ' + escapeHtml(req.method) + ' ' + escapeHtml(req.originalUrl) + '\n');122 // final handler123 done = done || finalhandler(req, res, {124 env: this.get('env'),125 onerror: logerror.bind(this)126 });127128 this._router.handle(req, res, done);129};130131/**132 * Proxy `Router#use()` to add middleware to the app router.
381 unmodified lines
514app.listen = function(){515 var server = http.createServer(this);516 return server.listen.apply(server, arguments);517};518519/**520* Log error using console.error.521*522* @param {Error} err523* @api public524*/525526function logerror(err){527 if (this.get('env') !== 'test') console.error(err.stack || err.toString());528}
modified:History.md+1 added
1unreleased2==========34 * deprecate things with `depd` module5 * use `finalhandler` for final response handling6 * use `media-typer` to alter content-type charset784.4.4 / 2014-06-209==================
1678 unmodified lines
modified:package.json+1 added