index.js
This file
Path
examples/error-pages/index.js
104 lines · 2.6 KiB · JavaScript
Last changed by
Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
History of this fileTake it
The walk stopped after 200 commits: lines older than 34c83d7 point at it.
3.4.7Jonathan Ongbefore adoption34c83d7
lines 1–4why, one step back1/**2 * Module dependencies.3 */4Merge pull request #2008 from agchou/clean-up-codeJonathan Ongbefore adoption2e830ff
lines 5–8why, one step back5var express = require('../../');6var app = module.exports = express();7var logger = require('morgan');8var silent = 'test' == process.env.NODE_ENV;3.4.7Jonathan Ongbefore adoption34c83d7
lines 9–20why, one step back910// general config11app.set('views', __dirname + '/views');12app.set('view engine', 'jade');1314// our custom "verbose errors" setting15// which we can use in the templates16// via settings['verbose errors']17app.enable('verbose errors');1819// disable them in production20// use $ NODE_ENV=production node examples/error-pagesMerge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
line 21why, one step back21if ('production' == app.settings.env) app.disable('verbose errors');3.4.7Jonathan Ongbefore adoption34c83d7
line 22why, one step back22remove last pieces of connect dependencyRoman Shtylmanbefore adoption1396e08
line 23why, one step back23silent || app.use(logger('dev'));3.4.7Jonathan Ongbefore adoption34c83d7
line 24why, one step back24remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
line 25why, one step back25// Routes3.4.7Jonathan Ongbefore adoption34c83d7
line 26why, one step back26remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 27–29why, one step back27app.get('/', function(req, res){28 res.render('index.jade');29});3.4.7Jonathan Ongbefore adoption34c83d7
line 30why, one step back30remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
lines 31–51why, one step back31app.get('/404', function(req, res, next){32 // trigger a 404 since no other middleware33 // will match /404 after this one, and we're not34 // responding here35 next();36});3738app.get('/403', function(req, res, next){39 // trigger a 403 error40 var err = new Error('not allowed!');41 err.status = 403;42 next(err);43});4445app.get('/500', function(req, res, next){46 // trigger a generic (500) error47 next(new Error('keyboard cat!'));48});4950// Error handlers513.4.7Jonathan Ongbefore adoption34c83d7
lines 52–61why, one step back52// Since this is the last non-error-handling53// middleware use()d, we assume 404, as nothing else54// responded.5556// $ curl http://localhost:3000/notfound57// $ curl http://localhost:3000/notfound -H "Accept: application/json"58// $ curl http://localhost:3000/notfound -H "Accept: text/plain"5960app.use(function(req, res, next){61 res.status(404);remove app.router and refactor middleware processingRoman Shtylmanbefore adoptionf41d09a
line 62why, one step back623.4.7Jonathan Ongbefore adoption34c83d7
lines 63–99why, one step back63 // respond with html page64 if (req.accepts('html')) {65 res.render('404', { url: req.url });66 return;67 }6869 // respond with json70 if (req.accepts('json')) {71 res.send({ error: 'Not found' });72 return;73 }7475 // default to plain-text. send()76 res.type('txt').send('Not found');77});7879// error-handling middleware, take the same form80// as regular middleware, however they require an81// arity of 4, aka the signature (err, req, res, next).82// when connect has an error, it will invoke ONLY error-handling83// middleware.8485// If we were to next() here any remaining non-error-handling86// middleware would then be executed, or if we next(err) to87// continue passing the error, only error-handling middleware88// would remain being executed, however here89// we simply respond with an error page.9091app.use(function(err, req, res, next){92 // we may use properties of the error object93 // here and next(err) appropriately, or if94 // we possibly recovered from the error, simply next().95 res.status(err.status || 500);96 res.render('500', { error: err });97});9899Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
line 100why, one step back100/* istanbul ignore next */3.4.7Jonathan Ongbefore adoption34c83d7
lines 101–102why, one step back101if (!module.parent) {102 app.listen(3000);Merge tag '3.9.0'Douglas Christopher Wilsonbefore adoptiondb4448d
line 103why, one step back103 console.log('Express started on port 3000');3.4.7Jonathan Ongbefore adoption34c83d7
line 104why, one step back104}