app.js
This file
Path
examples/error/app.js
45 lines · 1015 bytes · JavaScript
Last changed by
mergedTj Holowaychukbefore adoption547d18c
History of this fileTake it
The walk stopped after 200 commits: lines older than 7a24021 point at it.
more res.redirect() testsTj Holowaychukbefore adoption7a24021
lines 1–5why, one step back12/**3 * Module dependencies.4 */5mergedTj Holowaychukbefore adoption547d18c
lines 6–7why, one step back6var express = require('../../')7 , app = express();more res.redirect() testsTj Holowaychukbefore adoption7a24021
line 8why, one step back8mergedTj Holowaychukbefore adoption547d18c
lines 9–10why, one step back9app.use(express.logger('dev'));10app.use(app.router);more res.redirect() testsTj Holowaychukbefore adoption7a24021
line 11why, one step back11mergedTj Holowaychukbefore adoption547d18c
lines 12–31why, one step back12// the error handler is strategically13// placed *below* the app.router; if it14// were above it would not receive errors15// from app.get() etc 16app.use(error);1718// error handling middleware have an arity of 419// instead of the typical (req, res, next),20// otherwise they behave exactly like regular21// middleware, you may have several of them,22// in different orders etc.2324function error(err, req, res, next) {25 // log it26 console.error(err.stack);2728 // respond with 500 "Internal Server Error".29 res.send(500);30}31more res.redirect() testsTj Holowaychukbefore adoption7a24021
lines 32–38why, one step back32app.get('/', function(req, res){33 // Caught and passed down to the errorHandler middleware34 throw new Error('something broke!');35});3637app.get('/next', function(req, res, next){38 // We can also pass exceptions to next()mergedTj Holowaychukbefore adoption547d18c
lines 39–41why, one step back39 process.nextTick(function(){40 next(new Error('oh no!'));41 });more res.redirect() testsTj Holowaychukbefore adoption7a24021
lines 42–45why, one step back42});4344app.listen(3000);45console.log('app listening on port 3000');