Skip to content
Myr

app.js

This file

Path

examples/error-pages/app.js

96 lines · 2.2 KiB · JavaScript

Last changed by

docsTj Holowaychukbefore adoption6cae1f0

History of this file

Take it

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

Updated connect submoduleTj Holowaychukbefore adoptiona9db47f

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

docsTj Holowaychukbefore adoption6cae1f0

line 9why, one step back
9var express = require('../../lib/express');

Updated connect submoduleTj Holowaychukbefore adoptiona9db47f

lines 10–66why, one step back
10
11var app = express.createServer();1213// 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){34 next(new NotFound(req.url));35});3637app.set('views', __dirname + '/views');3839// Provide our app with the notion of NotFound exceptions4041function NotFound(path){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);50}5152/**53 * Inherit from `Error.prototype`.54 */5556NotFound.prototype.__proto__ = Error.prototype;5758// 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){

docsTj Holowaychukbefore adoption6cae1f0

lines 67–71why, one step back
67 if (err instanceof NotFound) {
68 res.render('404.jade', { status: 404, error: err });69 } else {70 next(err);71 }

Updated connect submoduleTj Holowaychukbefore adoptiona9db47f

lines 72–96why, one step back
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){78 res.render('500.jade', { status: 500, error: err });79});8081// Routes8283app.get('/', function(req, res){84 res.render('index.jade');85});8687app.get('/404', function(req, res){88 throw new NotFound(req.url);89});9091app.get('/500', function(req, res, next){92 next(new Error('keyboard cat!'));93});9495app.listen(3000);96console.log('Express app started on port 3000');