index.js
This file
Path
examples/error/index.js
48 lines · 1.1 KiB · JavaScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than 363d0d4 point at it.
Merge branch 'master' of github.com:visionmedia/expressTJ Holowaychukbefore adoption363d0d4
lines 1–48why, one step back12/**3 * Module dependencies.4 */56var express = require('../../')7 , app = module.exports = express()8 , test = app.get('env') == 'test';910if (!test) app.use(express.logger('dev'));11app.use(app.router);1213// the error handler is strategically14// placed *below* the app.router; if it15// were above it would not receive errors16// from app.get() etc 17app.use(error);1819// error handling middleware have an arity of 420// instead of the typical (req, res, next),21// otherwise they behave exactly like regular22// middleware, you may have several of them,23// in different orders etc.2425function error(err, req, res, next) {26 // log it27 if (!test) console.error(err.stack);2829 // respond with 500 "Internal Server Error".30 res.send(500);31}3233app.get('/', function(req, res){34 // Caught and passed down to the errorHandler middleware35 throw new Error('something broke!');36});3738app.get('/next', function(req, res, next){39 // We can also pass exceptions to next()40 process.nextTick(function(){41 next(new Error('oh no!'));42 });43});4445if (!module.parent) {46 app.listen(3000);47 console.log('Express started on port 3000');48}