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
Provenance
- Commit
7e01531- Tree
8d15d7a- Author
Douglas Christopher Wilson <doug@somethingdoug.com>- Committer
Douglas Christopher Wilson <doug@somethingdoug.com>- Parent
3ffceff
3 files changed+20 added−39 removed
modified:lib/application.js+18 added-39 removed
1 /**2 * Module dependencies.3 */4 5+var finalhandler = require('finalhandler');6 var mixin = require('utils-merge');6-var escapeHtml = require('escape-html');7 var Router = require('./router');8 var methods = require('methods');9 var middleware = require('./middleware/init');10 var query = require('./middleware/query');107 unmodified lines118 * @api private119 */120 121 app.handle = function(req, res, done) {122- var env = this.get('env');123-124- this._router.handle(req, res, function(err) {125- if (done) {126- return done(err);127- }128-129- // unhandled error130- if (err) {131- // default to 500132- if (res.statusCode < 400) res.statusCode = 500;133- debug('default %s', res.statusCode);134-135- // respect err.status136- if (err.status) res.statusCode = err.status;137-138- // production gets a basic error message139- var msg = 'production' == env140- ? http.STATUS_CODES[res.statusCode]141- : err.stack || err.toString();142- msg = escapeHtml(msg);143-144- // 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- }153-154- // 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 });127+128+ this._router.handle(req, res, done);129 };130 131 /**132 * Proxy `Router#use()` to add middleware to the app router.381 unmodified lines514 app.listen = function(){515 var server = http.createServer(this);516 return server.listen.apply(server, arguments);517 };518+519+/**520+* Log error using console.error.521+*522+* @param {Error} err523+* @api public524+*/525+526+function logerror(err){527+ if (this.get('env') !== 'test') console.error(err.stack || err.toString());528+}modified:History.md+1 added
1 unreleased2 ==========3 4 * deprecate things with `depd` module5+ * use `finalhandler` for final response handling6 * use `media-typer` to alter content-type charset7 8 4.4.4 / 2014-06-209 ==================1678 unmodified linesmodified:package.json+1 added
28 unmodified lines29303132333435363742 unmodified lines28 unmodified lines "buffer-crc32": "0.2.3", "debug": "1.0.2", "depd": "0.3.0", "escape-html": "1.0.1", "finalhandler": "0.0.0", "media-typer": "0.2.0", "methods": "1.0.1", "parseurl": "1.0.1", "proxy-addr": "1.0.1",42 unmodified lines