index.js
This file
Path
examples/error/index.js
48 lines · 1.1 KiB · JavaScript
Last changed by
docs: update examples for deprecationsDouglas Christopher Wilsonbefore adoptione8f8ea7
History of this fileTake it
The walk stopped after 200 commits: lines older than b89a597 point at it.
Restore req.params after invoking RouterDouglas Christopher Wilsonbefore adoptionb89a597
lines 1–22why, one step back1/**2 * Module dependencies.3 */45var express = require('../../');6var logger = require('morgan');7var app = module.exports = express();8var test = app.get('env') == 'test';910if (!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');Restore req.params after invoking RouterDouglas Christopher Wilsonbefore adoptionb89a597
lines 25–48why, 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()34 process.nextTick(function(){35 next(new Error('oh no!'));36 });37});3839// the error handler is placed after routes40// if it were above it would not receive errors41// from app.get() etc42app.use(error);4344/* istanbul ignore next */45if (!module.parent) {46 app.listen(3000);47 console.log('Express started on port 3000');48}