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
Provenance
- Commit
c17ad6e- Tree
5754679- Author
TJ Holowaychuk <tj@vision-media.ca>- Committer
TJ Holowaychuk <tj@vision-media.ca>- Parent
2c14d0c
2 files changed+95 added−95 removed
deleted:examples/error-pages/app.js-95 removed
1-2-/**3- * Module dependencies.4- */5-6-var express = require('../../')7- , app = module.exports = express()8- , silent = 'test' == process.env.NODE_ENV;9-10-app.set('views', __dirname + '/views');11-app.set('view engine', 'jade');12-13-app.use(express.favicon());14-15-silent || app.use(express.logger('dev'));16-17-// "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.23-24-app.use(app.router);25-26-// Since this is the last non-error-handling27-// middleware use()d, we assume 404, as nothing else28-// responded.29-30-// $ curl http://localhost:3000/notfound31-// $ curl http://localhost:3000/notfound -H "Accept: application/json"32-// $ curl http://localhost:3000/notfound -H "Accept: text/plain"33-34-app.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- }41-42- // respond with json43- if (req.accepts('json')) {44- res.send({ error: 'Not found' });45- return;46- }47-48- // default to plain-text. send()49- res.type('txt').send('Not found');50-});51-52-// 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.57-58-// 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.63-64-app.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-});71-72-// Routes73-74-app.get('/', function(req, res){75- res.render('index.jade');76-});77-78-app.get('/404', function(req, res, next){79- next();80-});81-82-app.get('/403', function(req, res, next){83- var err = new Error('not allowed!');84- err.status = 403;85- next(err);86-});87-88-app.get('/500', function(req, res, next){89- next(new Error('keyboard cat!'));90-});91-92-if (!module.parent) {93- app.listen(3000);94- silent || console.log('Express started on port 3000');95-}added:examples/error-pages/index.js+95 added
1+2+/**3+ * Module dependencies.4+ */5+6+var express = require('../../')7+ , app = module.exports = express()8+ , silent = 'test' == process.env.NODE_ENV;9+10+app.set('views', __dirname + '/views');11+app.set('view engine', 'jade');12+13+app.use(express.favicon());14+15+silent || app.use(express.logger('dev'));16+17+// "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.23+24+app.use(app.router);25+26+// Since this is the last non-error-handling27+// middleware use()d, we assume 404, as nothing else28+// responded.29+30+// $ curl http://localhost:3000/notfound31+// $ curl http://localhost:3000/notfound -H "Accept: application/json"32+// $ curl http://localhost:3000/notfound -H "Accept: text/plain"33+34+app.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+ }41+42+ // respond with json43+ if (req.accepts('json')) {44+ res.send({ error: 'Not found' });45+ return;46+ }47+48+ // default to plain-text. send()49+ res.type('txt').send('Not found');50+});51+52+// 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.57+58+// 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.63+64+app.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+});71+72+// Routes73+74+app.get('/', function(req, res){75+ res.render('index.jade');76+});77+78+app.get('/404', function(req, res, next){79+ next();80+});81+82+app.get('/403', function(req, res, next){83+ var err = new Error('not allowed!');84+ err.status = 403;85+ next(err);86+});87+88+app.get('/500', function(req, res, next){89+ next(new Error('keyboard cat!'));90+});91+92+if (!module.parent) {93+ app.listen(3000);94+ silent || console.log('Express started on port 3000');95+}