app.js
This file
Path
examples/error-pages/app.js
83 lines · 2.1 KiB · JavaScript
Last changed by
updated error-pages exampleTj Holowaychukbefore adoption6b47271
History of this fileTake it
The walk stopped after 200 commits: lines older than fda31b7 point at it.
Added simple web-service exampleTj Holowaychukbefore adoptionfda31b7
lines 1–11why, 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();updated error-pages exampleTj Holowaychukbefore adoption6b47271
lines 12–13why, one step back12app.set('views', __dirname + '/views');13app.set('view engine', 'jade');Added simple web-service exampleTj Holowaychukbefore adoptionfda31b7
lines 14–19why, one step back1415// Serve default connect favicon16app.use(express.favicon());1718// Logger is placed below favicon, so favicon.ico19// requests will not be loggedupdated error-pages exampleTj Holowaychukbefore adoption6b47271
line 20why, one step back20app.use(express.logger('":method :url" :status'));Added simple web-service exampleTj Holowaychukbefore adoptionfda31b7
lines 21–27why, one step back2122// "app.router" positions our routes 23// specifically above the middleware24// assigned below2526app.use(app.router);27updated error-pages exampleTj Holowaychukbefore adoption6b47271
lines 28–30why, one step back28// Since this is the last non-error-handling29// middleware use()d, we assume 404, as nothing else30// responded.Added simple web-service exampleTj Holowaychukbefore adoptionfda31b7
lines 31–32why, one step back3132app.use(function(req, res, next){updated error-pages exampleTj Holowaychukbefore adoption6b47271
lines 33–36why, one step back33 // 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 });Added simple web-service exampleTj Holowaychukbefore adoptionfda31b7
lines 37–38why, one step back37});38updated error-pages exampleTj Holowaychukbefore adoption6b47271
lines 39–43why, one step back39// 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.Added simple web-service exampleTj Holowaychukbefore adoptionfda31b7
line 44why, one step back44updated error-pages exampleTj Holowaychukbefore adoption6b47271
lines 45–49why, one step back45// 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.Added simple web-service exampleTj Holowaychukbefore adoptionfda31b7
lines 50–51why, one step back5051updated error-pages exampleTj Holowaychukbefore adoption6b47271
lines 52–59why, one step back52app.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 });Added simple web-service exampleTj Holowaychukbefore adoptionfda31b7
lines 60–67why, one step back60});6162// Routes6364app.get('/', function(req, res){65 res.render('index.jade');66});67updated error-pages exampleTj Holowaychukbefore adoption6b47271
lines 68–75why, one step back68app.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);Added simple web-service exampleTj Holowaychukbefore adoptionfda31b7
lines 76–83why, one step back76});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');