index.js
This file
Path
examples/error/index.js
53 lines · 1.3 KiB · JavaScript
Last changed by
Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
History of this fileTake it
The walk stopped after 200 commits: lines older than 2a0c35a point at it.
Merge tag '4.17.3'Douglas Christopher Wilsonbefore adoption318fd4b
lines 1–2why, one step back1'use strict'2Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 3–9why, one step back3/**4 * Module dependencies.5 */67var express = require('../../');8var logger = require('morgan');9var app = module.exports = express();Merge tag '4.16.4'Douglas Christopher Wilsonbefore adoption302a615
line 10why, one step back10var test = app.get('env') === 'test'Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 11–28why, one step back1112if (!test) app.use(logger('dev'));1314// error handling middleware have an arity of 415// instead of the typical (req, res, next),16// otherwise they behave exactly like regular17// middleware, you may have several of them,18// in different orders etc.1920function error(err, req, res, next) {21 // log it22 if (!test) console.error(err.stack);2324 // respond with 500 "Internal Server Error".25 res.status(500);26 res.send('Internal Server Error');27}28Merge tag '4.19.0' into 5.xWes Toddbefore adoptione9f9aae
line 29why, one step back29app.get('/', function () {Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 30–35why, one step back30 // Caught and passed down to the errorHandler middleware31 throw new Error('something broke!');32});3334app.get('/next', function(req, res, next){35 // We can also pass exceptions to next()Merge tag '4.14.0'Douglas Christopher Wilsonbefore adoption1dbfee6
lines 36–38why, one step back36 // The reason for process.nextTick() is to show that37 // next() can be called inside an async operation,38 // in real life it can be a DB read or HTTP request.Invoke callback for sendfile when client abortsDouglas Christopher Wilsonbefore adoption2a0c35a
lines 39–53why, one step back39 process.nextTick(function(){40 next(new Error('oh no!'));41 });42});4344// the error handler is placed after routes45// if it were above it would not receive errors46// from app.get() etc47app.use(error);4849/* istanbul ignore next */50if (!module.parent) {51 app.listen(3000);52 console.log('Express started on port 3000');53}