Skip to content
Myr

app.js

This file

Path

examples/error-pages/app.js

92 lines · 2.3 KiB · JavaScript

Last changed by

mergedTj Holowaychukbefore adoption547d18c

History of this file

Take it

The walk stopped after 200 commits: lines older than 17ccd55 point at it.

upgrade shouldTj Holowaychukbefore adoption17ccd55

lines 1–5why, one step back
1
2/**3 * Module dependencies.4 */5

mergedTj Holowaychukbefore adoption547d18c

lines 6–7why, one step back
6var express = require('../../')
7 , app = express();

upgrade shouldTj Holowaychukbefore adoption17ccd55

lines 8–12why, one step back
8
9app.set('views', __dirname + '/views');10app.set('view engine', 'jade');1112app.use(express.favicon());

mergedTj Holowaychukbefore adoption547d18c

line 13why, one step back
13app.use(express.logger('dev'));

upgrade shouldTj Holowaychukbefore adoption17ccd55

lines 14–15why, one step back
14
15// "app.router" positions our routes

mergedTj Holowaychukbefore adoption547d18c

lines 16–20why, one step back
16// above the middleware defined below,
17// this means that Express will attempt18// to match & call routes _before_ continuing19// on, at which point we assume it's a 404 because20// no route has handled the request.

upgrade shouldTj Holowaychukbefore adoption17ccd55

lines 21–27why, one step back
21
22app.use(app.router);2324// Since this is the last non-error-handling25// middleware use()d, we assume 404, as nothing else26// responded.27

mergedTj Holowaychukbefore adoption547d18c

lines 28–31why, one step back
28// $ curl http://localhost:3000/notfound
29// $ curl http://localhost:3000/notfound -H "Accept: application/json"30// $ curl http://localhost:3000/notfound -H "Accept: text/plain"31

upgrade shouldTj Holowaychukbefore adoption17ccd55

line 32why, one step back
32app.use(function(req, res, next){

mergedTj Holowaychukbefore adoption547d18c

lines 33–48why, one step back
33 // respond with html page
34 if (req.accepts('html')) {35 console.log('test');36 res.status(404);37 res.render('404', { url: req.url });38 return;39 }4041 // respond with json42 if (req.accepts('json')) {43 res.send({ error: 'Not found' });44 return;45 }4647 // default to plain-text. send()48 res.type('txt').send('Not found');

upgrade shouldTj Holowaychukbefore adoption17ccd55

lines 49–66why, one step back
49});
5051// error-handling middleware, take the same form52// as regular middleware, however they require an53// arity of 4, aka the signature (err, req, res, next).54// when connect has an error, it will invoke ONLY error-handling55// middleware.5657// If we were to next() here any remaining non-error-handling58// middleware would then be executed, or if we next(err) to59// continue passing the error, only error-handling middleware60// would remain being executed, however here61// we simply respond with an error page.6263app.use(function(err, req, res, next){64 // we may use properties of the error object65 // here and next(err) appropriately, or if66 // we possibly recovered from the error, simply next().

mergedTj Holowaychukbefore adoption547d18c

lines 67–68why, one step back
67 res.status(err.status || 500);
68 res.render('500', { error: err });

upgrade shouldTj Holowaychukbefore adoption17ccd55

lines 69–92why, one step back
69});
7071// Routes7273app.get('/', function(req, res){74 res.render('index.jade');75});7677app.get('/404', function(req, res, next){78 next();79});8081app.get('/403', function(req, res, next){82 var err = new Error('not allowed!');83 err.status = 403;84 next(err);85});8687app.get('/500', function(req, res, next){88 next(new Error('keyboard cat!'));89});9091app.listen(3000);92console.log('Express app started on port 3000');