index.js
This file
Path
examples/error/index.js
47 lines · 1.1 KiB · JavaScript
Last changed by
Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
History of this fileTake it
The walk stopped after 200 commits: lines older than 718e68f point at it.
use utils-merge module to mixin object propertiesRoman Shtylmanbefore adoption718e68f
lines 1–4why, one step back1/**2 * Module dependencies.3 */4Merge pull request #2008 from agchou/clean-up-codeJonathan Ongbefore adoption2e830ff
lines 5–8why, one step back5var express = require('../../');6var logger = require('morgan');7var app = module.exports = express();8var test = app.get('env') == 'test';use utils-merge module to mixin object propertiesRoman Shtylmanbefore adoption718e68f
line 9why, one step back9remove last pieces of connect dependencyRoman Shtylmanbefore adoption1396e08
line 10why, one step back10if (!test) app.use(logger('dev'));use utils-merge module to mixin object propertiesRoman Shtylmanbefore adoption718e68f
lines 11–36why, one step back1112// error handling middleware have an arity of 413// instead of the typical (req, res, next),14// otherwise they behave exactly like regular15// middleware, you may have several of them,16// in different orders etc.1718function error(err, req, res, next) {19 // log it20 if (!test) console.error(err.stack);2122 // respond with 500 "Internal Server Error".23 res.send(500);24}2526app.get('/', function(req, res){27 // Caught and passed down to the errorHandler middleware28 throw new Error('something broke!');29});3031app.get('/next', function(req, res, next){32 // We can also pass exceptions to next()33 process.nextTick(function(){34 next(new Error('oh no!'));35 });36});remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 37–41why, one step back3738// the error handler is placed after routes39// if it were above it would not receive errors40// from app.get() etc41app.use(error);use utils-merge module to mixin object propertiesRoman Shtylmanbefore adoption718e68f
line 42why, one step back42Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
line 43why, one step back43/* istanbul ignore next */use utils-merge module to mixin object propertiesRoman Shtylmanbefore adoption718e68f
lines 44–47why, one step back44if (!module.parent) {45 app.listen(3000);46 console.log('Express started on port 3000');47}