index.js
This file
Path
examples/error/index.js
51 lines · 1.3 KiB · JavaScript
Last changed by
Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
History of this fileTake it
The walk stopped after 200 commits: lines older than d29cf4d point at it.
deps: serve-static@~1.3.2Douglas Christopher Wilsonbefore adoptiond29cf4d
lines 1–7why, one step back1/**2 * Module dependencies.3 */45var express = require('../../');6var logger = require('morgan');7var app = module.exports = express();Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
line 8why, one step back8var test = app.get('env') === 'test'deps: serve-static@~1.3.2Douglas Christopher Wilsonbefore adoptiond29cf4d
lines 9–22why, one step back910if (!test) app.use(logger('dev'));1112// 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".docs: update examples for deprecationsDouglas Christopher Wilsonbefore adoptione8f8ea7
lines 23–24why, one step back23 res.status(500);24 res.send('Internal Server Error');deps: serve-static@~1.3.2Douglas Christopher Wilsonbefore adoptiond29cf4d
lines 25–33why, one step back25}2627app.get('/', function(req, res){28 // Caught and passed down to the errorHandler middleware29 throw new Error('something broke!');30});3132app.get('/next', function(req, res, next){33 // We can also pass exceptions to next()Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 34–36why, one step back34 // The reason for process.nextTick() is to show that35 // next() can be called inside an async operation,36 // in real life it can be a DB read or HTTP request.deps: serve-static@~1.3.2Douglas Christopher Wilsonbefore adoptiond29cf4d
lines 37–51why, one step back37 process.nextTick(function(){38 next(new Error('oh no!'));39 });40});4142// the error handler is placed after routes43// if it were above it would not receive errors44// from app.get() etc45app.use(error);4647/* istanbul ignore next */48if (!module.parent) {49 app.listen(3000);50 console.log('Express started on port 3000');51}