Skip to content
Myr

error-pages/index.js

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by TJ Holowaychuk · committed by TJ Holowaychuk · parent 2c14d0c · Browse files · 2 files+95 added−95 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+95 added−95 removed

  • other:+95 added−95 removed

2 files+95 added−95 removed

deleted:examples/error-pages/app.js-95 removed
12/**3 * Module dependencies.4 */56var express = require('../../')7 , app = module.exports = express()8 , silent = 'test' == process.env.NODE_ENV;910app.set('views', __dirname + '/views');11app.set('view engine', 'jade');1213app.use(express.favicon());1415silent || app.use(express.logger('dev'));1617// "app.router" positions our routes 18// above the middleware defined below,19// this means that Express will attempt20// to match & call routes _before_ continuing21// on, at which point we assume it's a 404 because22// no route has handled the request.2324app.use(app.router);2526// Since this is the last non-error-handling27// middleware use()d, we assume 404, as nothing else28// responded.2930// $ curl http://localhost:3000/notfound31// $ curl http://localhost:3000/notfound -H "Accept: application/json"32// $ curl http://localhost:3000/notfound -H "Accept: text/plain"3334app.use(function(req, res, next){35 // respond with html page36 if (req.accepts('html')) {37 res.status(404);38 res.render('404', { url: req.url });39 return;40 }4142 // respond with json43 if (req.accepts('json')) {44 res.send({ error: 'Not found' });45 return;46 }4748 // default to plain-text. send()49 res.type('txt').send('Not found');50});5152// error-handling middleware, take the same form53// as regular middleware, however they require an54// arity of 4, aka the signature (err, req, res, next).55// when connect has an error, it will invoke ONLY error-handling56// middleware.5758// If we were to next() here any remaining non-error-handling59// middleware would then be executed, or if we next(err) to60// continue passing the error, only error-handling middleware61// would remain being executed, however here62// we simply respond with an error page.6364app.use(function(err, req, res, next){65 // we may use properties of the error object66 // here and next(err) appropriately, or if67 // we possibly recovered from the error, simply next().68 res.status(err.status || 500);69 res.render('500', { error: err });70});7172// Routes7374app.get('/', function(req, res){75 res.render('index.jade');76});7778app.get('/404', function(req, res, next){79 next();80});8182app.get('/403', function(req, res, next){83 var err = new Error('not allowed!');84 err.status = 403;85 next(err);86});8788app.get('/500', function(req, res, next){89 next(new Error('keyboard cat!'));90});9192if (!module.parent) {93 app.listen(3000);94 silent || console.log('Express started on port 3000');95}
added:examples/error-pages/index.js+95 added
12/**3 * Module dependencies.4 */56var express = require('../../')7 , app = module.exports = express()8 , silent = 'test' == process.env.NODE_ENV;910app.set('views', __dirname + '/views');11app.set('view engine', 'jade');1213app.use(express.favicon());1415silent || app.use(express.logger('dev'));1617// "app.router" positions our routes 18// above the middleware defined below,19// this means that Express will attempt20// to match & call routes _before_ continuing21// on, at which point we assume it's a 404 because22// no route has handled the request.2324app.use(app.router);2526// Since this is the last non-error-handling27// middleware use()d, we assume 404, as nothing else28// responded.2930// $ curl http://localhost:3000/notfound31// $ curl http://localhost:3000/notfound -H "Accept: application/json"32// $ curl http://localhost:3000/notfound -H "Accept: text/plain"3334app.use(function(req, res, next){35 // respond with html page36 if (req.accepts('html')) {37 res.status(404);38 res.render('404', { url: req.url });39 return;40 }4142 // respond with json43 if (req.accepts('json')) {44 res.send({ error: 'Not found' });45 return;46 }4748 // default to plain-text. send()49 res.type('txt').send('Not found');50});5152// error-handling middleware, take the same form53// as regular middleware, however they require an54// arity of 4, aka the signature (err, req, res, next).55// when connect has an error, it will invoke ONLY error-handling56// middleware.5758// If we were to next() here any remaining non-error-handling59// middleware would then be executed, or if we next(err) to60// continue passing the error, only error-handling middleware61// would remain being executed, however here62// we simply respond with an error page.6364app.use(function(err, req, res, next){65 // we may use properties of the error object66 // here and next(err) appropriately, or if67 // we possibly recovered from the error, simply next().68 res.status(err.status || 500);69 res.render('500', { error: err });70});7172// Routes7374app.get('/', function(req, res){75 res.render('index.jade');76});7778app.get('/404', function(req, res, next){79 next();80});8182app.get('/403', function(req, res, next){83 var err = new Error('not allowed!');84 err.status = 403;85 next(err);86});8788app.get('/500', function(req, res, next){89 next(new Error('keyboard cat!'));90});9192if (!module.parent) {93 app.listen(3000);94 silent || console.log('Express started on port 3000');95}