index.js
This file
Path
examples/error-pages/index.js
108 lines · 2.7 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 d853c83 point at it.
Release 3.1.0TJ Holowaychukbefore adoptiond853c83
lines 1–6why, one step back1/**2 * Module dependencies.3 */45var express = require('../../')6 , app = module.exports = express()remove last pieces of connect dependencyRoman Shtylmanbefore adoption1396e08
lines 7–8why, one step back7 , logger = require('morgan')8 , favicon = require('static-favicon')Release 3.1.0TJ Holowaychukbefore adoptiond853c83
lines 9–25why, one step back9 , silent = 'test' == process.env.NODE_ENV;1011// general config12app.set('views', __dirname + '/views');13app.set('view engine', 'jade');1415// our custom "verbose errors" setting16// which we can use in the templates17// via settings['verbose errors']18app.enable('verbose errors');1920// disable them in production21// use $ NODE_ENV=production node examples/error-pages22if ('production' == app.settings.env) {23 app.disable('verbose errors');24}25remove last pieces of connect dependencyRoman Shtylmanbefore adoption1396e08
line 26why, one step back26app.use(favicon());Release 3.1.0TJ Holowaychukbefore adoptiond853c83
line 27why, one step back27remove last pieces of connect dependencyRoman Shtylmanbefore adoption1396e08
line 28why, one step back28silent || app.use(logger('dev'));Release 3.1.0TJ Holowaychukbefore adoptiond853c83
line 29why, one step back29remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
line 30why, one step back30// RoutesRelease 3.1.0TJ Holowaychukbefore adoptiond853c83
line 31why, one step back31remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 32–34why, one step back32app.get('/', function(req, res){33 res.render('index.jade');34});Release 3.1.0TJ Holowaychukbefore adoptiond853c83
line 35why, one step back35remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 36–56why, one step back36app.get('/404', function(req, res, next){37 // trigger a 404 since no other middleware38 // will match /404 after this one, and we're not39 // responding here40 next();41});4243app.get('/403', function(req, res, next){44 // trigger a 403 error45 var err = new Error('not allowed!');46 err.status = 403;47 next(err);48});4950app.get('/500', function(req, res, next){51 // trigger a generic (500) error52 next(new Error('keyboard cat!'));53});5455// Error handlers56Release 3.1.0TJ Holowaychukbefore adoptiond853c83
lines 57–66why, one step back57// Since this is the last non-error-handling58// middleware use()d, we assume 404, as nothing else59// responded.6061// $ curl http://localhost:3000/notfound62// $ curl http://localhost:3000/notfound -H "Accept: application/json"63// $ curl http://localhost:3000/notfound -H "Accept: text/plain"6465app.use(function(req, res, next){66 res.status(404);remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
line 67why, one step back67Release 3.1.0TJ Holowaychukbefore adoptiond853c83
lines 68–108why, one step back68 // respond with html page69 if (req.accepts('html')) {70 res.render('404', { url: req.url });71 return;72 }7374 // respond with json75 if (req.accepts('json')) {76 res.send({ error: 'Not found' });77 return;78 }7980 // default to plain-text. send()81 res.type('txt').send('Not found');82});8384// error-handling middleware, take the same form85// as regular middleware, however they require an86// arity of 4, aka the signature (err, req, res, next).87// when connect has an error, it will invoke ONLY error-handling88// middleware.8990// If we were to next() here any remaining non-error-handling91// middleware would then be executed, or if we next(err) to92// continue passing the error, only error-handling middleware93// would remain being executed, however here94// we simply respond with an error page.9596app.use(function(err, req, res, next){97 // we may use properties of the error object98 // here and next(err) appropriately, or if99 // we possibly recovered from the error, simply next().100 res.status(err.status || 500);101 res.render('500', { error: err });102});103104105if (!module.parent) {106 app.listen(3000);107 silent || console.log('Express started on port 3000');108}