app.js
This file
Path
examples/error-pages/app.js
96 lines · 2.4 KiB · JavaScript
Last changed by
Updated error-pages example with acceptance test.Robert Sköldbefore adoption550b910
History of this fileTake it
The walk stopped after 200 commits: lines older than f98c112 point at it.
Added app.render() local precedence testTj Holowaychukbefore adoptionf98c112
lines 1–5why, one step back12/**3 * Module dependencies.4 */5mergedTj Holowaychukbefore adoption547d18c
line 6why, one step back6var express = require('../../')Updated error-pages example with acceptance test.Robert Sköldbefore adoption550b910
lines 7–8why, one step back7 , app = module.exports = express()8 , silent = 'test' == process.env.NODE_ENV;Added app.render() local precedence testTj Holowaychukbefore adoptionf98c112
lines 9–14why, one step back910app.set('views', __dirname + '/views');11app.set('view engine', 'jade');1213app.use(express.favicon());14Updated error-pages example with acceptance test.Robert Sköldbefore adoption550b910
lines 15–16why, one step back15silent || app.use(express.logger('dev'));16Added app.render() local precedence testTj Holowaychukbefore adoptionf98c112
line 17why, one step back17// "app.router" positions our routes mergedTj Holowaychukbefore adoption547d18c
lines 18–22why, one step back18// 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.Added app.render() local precedence testTj Holowaychukbefore adoptionf98c112
lines 23–29why, one step back2324app.use(app.router);2526// Since this is the last non-error-handling27// middleware use()d, we assume 404, as nothing else28// responded.29mergedTj Holowaychukbefore adoption547d18c
lines 30–33why, one step back30// $ curl http://localhost:3000/notfound31// $ curl http://localhost:3000/notfound -H "Accept: application/json"32// $ curl http://localhost:3000/notfound -H "Accept: text/plain"33Added app.render() local precedence testTj Holowaychukbefore adoptionf98c112
line 34why, one step back34app.use(function(req, res, next){mergedTj Holowaychukbefore adoption547d18c
lines 35–36why, one step back35 // respond with html page36 if (req.accepts('html')) {Updated error-pages example with acceptance test.Robert Sköldbefore adoption550b910
line 37why, one step back37 silent || console.log('test');mergedTj Holowaychukbefore adoption547d18c
lines 38–50why, one step back38 res.status(404);39 res.render('404', { url: req.url });40 return;41 }4243 // respond with json44 if (req.accepts('json')) {45 res.send({ error: 'Not found' });46 return;47 }4849 // default to plain-text. send()50 res.type('txt').send('Not found');Added app.render() local precedence testTj Holowaychukbefore adoptionf98c112
lines 51–68why, one step back51});5253// error-handling middleware, take the same form54// as regular middleware, however they require an55// arity of 4, aka the signature (err, req, res, next).56// when connect has an error, it will invoke ONLY error-handling57// middleware.5859// If we were to next() here any remaining non-error-handling60// middleware would then be executed, or if we next(err) to61// continue passing the error, only error-handling middleware62// would remain being executed, however here63// we simply respond with an error page.6465app.use(function(err, req, res, next){66 // we may use properties of the error object67 // here and next(err) appropriately, or if68 // we possibly recovered from the error, simply next().mergedTj Holowaychukbefore adoption547d18c
lines 69–70why, one step back69 res.status(err.status || 500);70 res.render('500', { error: err });Added app.render() local precedence testTj Holowaychukbefore adoptionf98c112
lines 71–92why, one step back71});7273// Routes7475app.get('/', function(req, res){76 res.render('index.jade');77});7879app.get('/404', function(req, res, next){80 next();81});8283app.get('/403', function(req, res, next){84 var err = new Error('not allowed!');85 err.status = 403;86 next(err);87});8889app.get('/500', function(req, res, next){90 next(new Error('keyboard cat!'));91});92Updated error-pages example with acceptance test.Robert Sköldbefore adoption550b910
lines 93–96why, one step back93if (!module.parent) {94 app.listen(3000);95 silent || console.log('Express started on port 3000');96}