index.js
This file
Path
examples/error-pages/index.js
112 lines · 2.8 KiB · JavaScript
Last changed by
add "verbose errors" option to error-pages exampleTJ Holowaychukbefore adoption3c5ad75
History of this fileTake it
The walk stopped after 200 commits: lines older than 214f913 point at it.
mergeTJ Holowaychukbefore adoption214f913
lines 1–8why, one step back1/**2 * Module dependencies.3 */45var express = require('../../')6 , app = module.exports = express()7 , silent = 'test' == process.env.NODE_ENV;8add "verbose errors" option to error-pages exampleTJ Holowaychukbefore adoption3c5ad75
line 9why, one step back9// general configmergeTJ Holowaychukbefore adoption214f913
lines 10–12why, one step back10app.set('views', __dirname + '/views');11app.set('view engine', 'jade');12add "verbose errors" option to error-pages exampleTJ Holowaychukbefore adoption3c5ad75
lines 13–23why, one step back13// our custom "verbose errors" setting14// which we can use in the templates15// via settings['verbose errors']16app.enable('verbose errors');1718// disable them in production19// use $ NODE_ENV=production node examples/error-pages20if ('production' == app.settings.env) {21 app.disable('verbose errors');22}23mergeTJ Holowaychukbefore adoption214f913
lines 24–45why, one step back24app.use(express.favicon());2526silent || app.use(express.logger('dev'));2728// "app.router" positions our routes 29// above the middleware defined below,30// this means that Express will attempt31// to match & call routes _before_ continuing32// on, at which point we assume it's a 404 because33// no route has handled the request.3435app.use(app.router);3637// Since this is the last non-error-handling38// middleware use()d, we assume 404, as nothing else39// responded.4041// $ curl http://localhost:3000/notfound42// $ curl http://localhost:3000/notfound -H "Accept: application/json"43// $ curl http://localhost:3000/notfound -H "Accept: text/plain"4445app.use(function(req, res, next){Merge pull request #1309 from nickjj/patch-1TJ Holowaychukbefore adoptiondd338b5
lines 46–47why, one step back46 res.status(404);47 mergeTJ Holowaychukbefore adoption214f913
lines 48–90why, one step back48 // respond with html page49 if (req.accepts('html')) {50 res.render('404', { url: req.url });51 return;52 }5354 // respond with json55 if (req.accepts('json')) {56 res.send({ error: 'Not found' });57 return;58 }5960 // default to plain-text. send()61 res.type('txt').send('Not found');62});6364// error-handling middleware, take the same form65// as regular middleware, however they require an66// arity of 4, aka the signature (err, req, res, next).67// when connect has an error, it will invoke ONLY error-handling68// middleware.6970// If we were to next() here any remaining non-error-handling71// middleware would then be executed, or if we next(err) to72// continue passing the error, only error-handling middleware73// would remain being executed, however here74// we simply respond with an error page.7576app.use(function(err, req, res, next){77 // we may use properties of the error object78 // here and next(err) appropriately, or if79 // we possibly recovered from the error, simply next().80 res.status(err.status || 500);81 res.render('500', { error: err });82});8384// Routes8586app.get('/', function(req, res){87 res.render('index.jade');88});8990app.get('/404', function(req, res, next){add some comments to error-pages exampleTJ Holowaychukbefore adoptionf7c1c63
lines 91–93why, one step back91 // trigger a 404 since no other middleware92 // will match /404 after this one, and we're not93 // responding heremergeTJ Holowaychukbefore adoption214f913
lines 94–97why, one step back94 next();95});9697app.get('/403', function(req, res, next){add some comments to error-pages exampleTJ Holowaychukbefore adoptionf7c1c63
line 98why, one step back98 // trigger a 403 errormergeTJ Holowaychukbefore adoption214f913
lines 99–104why, one step back99 var err = new Error('not allowed!');100 err.status = 403;101 next(err);102});103104app.get('/500', function(req, res, next){add some comments to error-pages exampleTJ Holowaychukbefore adoptionf7c1c63
line 105why, one step back105 // trigger a generic (500) errormergeTJ Holowaychukbefore adoption214f913
lines 106–112why, one step back106 next(new Error('keyboard cat!'));107});108109if (!module.parent) {110 app.listen(3000);111 silent || console.log('Express started on port 3000');112}