index.js
This file
Path
examples/error/index.js
47 lines · 1.1 KiB · JavaScript
Last changed by
remove last pieces of connect dependencyRoman Shtylmanbefore adoption1396e08
History of this fileTake it
The walk stopped after 200 commits: lines older than 15496da point at it.
remove ./client.jsTJ Holowaychukbefore adoption15496da
lines 1–6why, one step back12/**3 * Module dependencies.4 */56var express = require('../../')remove last pieces of connect dependencyRoman Shtylmanbefore adoption1396e08
line 7why, one step back7 , logger = require('morgan')remove ./client.jsTJ Holowaychukbefore adoption15496da
lines 8–10why, one step back8 , app = module.exports = express()9 , test = app.get('env') == 'test';10remove last pieces of connect dependencyRoman Shtylmanbefore adoption1396e08
line 11why, one step back11if (!test) app.use(logger('dev'));remove ./client.jsTJ Holowaychukbefore adoption15496da
lines 12–37why, one step back1213// error handling middleware have an arity of 414// instead of the typical (req, res, next),15// otherwise they behave exactly like regular16// middleware, you may have several of them,17// in different orders etc.1819function error(err, req, res, next) {20 // log it21 if (!test) console.error(err.stack);2223 // respond with 500 "Internal Server Error".24 res.send(500);25}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});remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 38–42why, one step back3839// the error handler is placed after routes40// if it were above it would not receive errors41// from app.get() etc42app.use(error);remove ./client.jsTJ Holowaychukbefore adoption15496da
lines 43–47why, one step back4344if (!module.parent) {45 app.listen(3000);46 console.log('Express started on port 3000');47}