index.js
This file
Path
examples/error-pages/index.js
103 lines · 2.6 KiB · JavaScript
Last changed by
Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
History of this fileTake it
The walk stopped after 200 commits: lines older than 6bc7574 point at it.
docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 1–11why, one step back1/**2 * Module dependencies.3 */45var express = require('../../');6var app = module.exports = express();7var logger = require('morgan');8var silent = 'test' == process.env.NODE_ENV;910// general config11app.set('views', __dirname + '/views');Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 12why, one step back12app.set('view engine', 'ejs');docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 13–27why, one step back1314// 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-pages21if ('production' == app.settings.env) app.disable('verbose errors');2223silent || app.use(logger('dev'));2425// Routes2627app.get('/', function(req, res){Merge tag '3.18.6'Douglas Christopher Wilsonbefore adoption4aa2801
line 28why, one step back28 res.render('index.ejs');docs: typo fixesDouglas Christopher Wilsonbefore adoption6bc7574
lines 29–103why, one step back29});3031app.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 handlers5152// 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);6263 // 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});9899/* istanbul ignore next */100if (!module.parent) {101 app.listen(3000);102 console.log('Express started on port 3000');103}