Skip to content
Myr

app.js

This file

Path

examples/error-pages/app.js

96 lines · 2.2 KiB · JavaScript

Last changed by

refactored error-pages exampleTj Holowaychukbefore adoption1ac18f7

History of this file

Take it

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

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 1–10why, one step back
1
2// Expose modules in ./support for demo purposes3require.paths.unshift(__dirname + '/../../support');45/**6 * Module dependencies.7 */89var express = require('./../../lib/express');10

refactored error-pages exampleTj Holowaychukbefore adoption1ac18f7

line 11why, one step back
11var app = express.createServer();

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 12–33why, one step back
12
13// Serve default connect favicon14app.use(express.favicon());1516// Logger is placed below favicon, so favicon.ico17// requests will not be logged18app.use(express.logger({ format: '":method :url" :status' }));1920// "app.router" positions our routes 21// specifically above the middleware22// assigned below2324app.use(app.router);2526// When no more middleware require execution, aka27// our router is finished and did not respond, we28// can assume that it is "not found". Instead of29// letting Connect deal with this, we define our30// custom middleware here to simply pass a NotFound31// exception3233app.use(function(req, res, next){

refactored error-pages exampleTj Holowaychukbefore adoption1ac18f7

line 34why, one step back
34 next(new NotFound(req.url));

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 35–41why, one step back
35});
3637app.set('views', __dirname + '/views');3839// Provide our app with the notion of NotFound exceptions4041function NotFound(path){

refactored error-pages exampleTj Holowaychukbefore adoption1ac18f7

lines 42–49why, one step back
42 this.name = 'NotFound';
43 if (path) {44 Error.call(this, 'Cannot find ' + path);45 this.path = path;46 } else {47 Error.call(this, 'Not Found');48 }49 Error.captureStackTrace(this, arguments.callee);

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 50–51why, one step back
50}
51

refactored error-pages exampleTj Holowaychukbefore adoption1ac18f7

lines 52–54why, one step back
52/**
53 * Inherit from `Error.prototype`.54 */

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 55why, one step back
55

refactored error-pages exampleTj Holowaychukbefore adoption1ac18f7

lines 56–57why, one step back
56NotFound.prototype.__proto__ = Error.prototype;
57

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 58–67why, one step back
58// We can call app.error() several times as shown below.
59// Here we check for an instanceof NotFound and show the60// 404 page, or we pass on to the next error handler.6162// These handlers could potentially be defined within63// configure() blocks to provide introspection when64// in the development environment.6566app.error(function(err, req, res, next){67 if (err instanceof NotFound) {

refactored error-pages exampleTj Holowaychukbefore adoption1ac18f7

line 68why, one step back
68 res.render('404.jade', { status: 404, error: err });

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

line 69why, one step back
69 } else {

refactored error-pages exampleTj Holowaychukbefore adoption1ac18f7

line 70why, one step back
70 next(err);

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 71–77why, one step back
71 }
72});7374// Here we assume all errors as 500 for the simplicity of75// this demo, however you can choose whatever you like7677app.error(function(err, req, res){

refactored error-pages exampleTj Holowaychukbefore adoption1ac18f7

line 78why, one step back
78 res.render('500.jade', { status: 500, error: err });

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 79–83why, one step back
79});
8081// Routes8283app.get('/', function(req, res){

refactored error-pages exampleTj Holowaychukbefore adoption1ac18f7

line 84why, one step back
84 res.render('index.jade');

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 85–87why, one step back
85});
8687app.get('/404', function(req, res){

refactored error-pages exampleTj Holowaychukbefore adoption1ac18f7

line 88why, one step back
88 throw new NotFound(req.url);

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 89–91why, one step back
89});
9091app.get('/500', function(req, res, next){

refactored error-pages exampleTj Holowaychukbefore adoption1ac18f7

line 92why, one step back
92 next(new Error('keyboard cat!'));

master is 2.0.0-preTj Holowaychukbefore adoptionce2a18f

lines 93–96why, one step back
93});
9495app.listen(3000);96console.log('Express app started on port 3000');