app.js
This file
Path
examples/error-pages/app.js
83 lines · 2.1 KiB · JavaScript
Last changed by
Not known.
History of this fileTake it
The walk stopped after 200 commits: lines older than cd88b92 point at it.
Removed `app.dynamicLocals()`. Closes #756Tj Holowaychukbefore adoptioncd88b92
lines 1–83why, one step back12// Expose modules in ./support for demo purposes3require.paths.unshift(__dirname + '/../../support');45/**6 * Module dependencies.7 */89var express = require('../../lib/express');1011var app = express.createServer();12app.set('views', __dirname + '/views');13app.set('view engine', 'jade');1415// Serve default connect favicon16app.use(express.favicon());1718// Logger is placed below favicon, so favicon.ico19// requests will not be logged20app.use(express.logger('":method :url" :status'));2122// "app.router" positions our routes 23// specifically above the middleware24// assigned below2526app.use(app.router);2728// Since this is the last non-error-handling29// middleware use()d, we assume 404, as nothing else30// responded.3132app.use(function(req, res, next){33 // the status option, or res.statusCode = 40434 // are equivalent, however with the option we35 // get the "status" local available as well36 res.render('404', { status: 404, url: req.url });37});3839// error-handling middleware, take the same form40// as regular middleware, however they require an41// arity of 4, aka the signature (err, req, res, next).42// when connect has an error, it will invoke ONLY error-handling43// middleware.4445// If we were to next() here any remaining non-error-handling46// middleware would then be executed, or if we next(err) to47// continue passing the error, only error-handling middleware48// would remain being executed, however here49// we simply respond with an error page.505152app.use(function(err, req, res, next){53 // we may use properties of the error object54 // here and next(err) appropriately, or if55 // we possibly recovered from the error, simply next().56 res.render('500', {57 status: err.status || 50058 , error: err59 });60});6162// Routes6364app.get('/', function(req, res){65 res.render('index.jade');66});6768app.get('/404', function(req, res, next){69 next();70});7172app.get('/403', function(req, res, next){73 var err = new Error('not allowed!');74 err.status = 403;75 next(err);76});7778app.get('/500', function(req, res, next){79 next(new Error('keyboard cat!'));80});8182app.listen(3000);83console.log('Express app started on port 3000');