refactored error-pages example
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 4f4987e · Browse files · 1 file·+21 added−28 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+21 added−28 removed
- other:+21 added−28 removed
Provenance
- Commit
1ac18f7- Tree
bcbbb46- Author
Tj Holowaychuk <tj@vision-media.ca>- Committer
Tj Holowaychuk <tj@vision-media.ca>- Parent
4f4987e
1 file changed+21 added−28 removed
modified:examples/error-pages/app.js+21 added-28 removed
6 unmodified lines789101112111213141514 unmodified lines3031323335343536373839404143444546474849504243444546474849505153525354555657585960612 unmodified lines646566676566676869706869727071727374757677808182838485787980818283918485868795888990919992939495966 unmodified lines */
var express = require('./../../lib/express');
var app = express.createServer(), sys = require('sys');var app = express.createServer();
// Serve default connect faviconapp.use(express.favicon());
14 unmodified lines// custom middleware here to simply pass a NotFound// exception
app.use(function(req, res, next){ next(new NotFound(req.url)); next(new NotFound(req.url));});
app.set('views', __dirname + '/views');
// Provide our app with the notion of NotFound exceptions
function NotFound(path){ this.name = 'NotFound'; if (path) { Error.call(this, 'Cannot find ' + path); this.path = path; } else { Error.call(this, 'Not Found'); } Error.captureStackTrace(this, arguments.callee); this.name = 'NotFound'; if (path) { Error.call(this, 'Cannot find ' + path); this.path = path; } else { Error.call(this, 'Not Found'); } Error.captureStackTrace(this, arguments.callee);}
sys.inherits(NotFound, Error);/** * Inherit from `Error.prototype`. */
NotFound.prototype.__proto__ = Error.prototype;
// We can call app.error() several times as shown below.// Here we check for an instanceof NotFound and show the// 404 page, or we pass on to the next error handler.
2 unmodified lines// in the development environment.
app.error(function(err, req, res, next){ if (err instanceof NotFound) { res.render('404.jade', { status: 404, locals: { error: err } }); res.render('404.jade', { status: 404, error: err }); } else { next(err); next(err); }});
// Here we assume all errors as 500 for the simplicity of// this demo, however you can choose whatever you like
app.error(function(err, req, res){ res.render('500.jade', { status: 500, locals: { error: err } }); res.render('500.jade', { status: 500, error: err });});
// Routes
app.get('/', function(req, res){ res.render('index.jade'); res.render('index.jade');});
app.get('/404', function(req, res){ throw new NotFound; throw new NotFound(req.url);});
app.get('/500', function(req, res, next){ next(new Error('keyboard cat!')); next(new Error('keyboard cat!'));});
app.listen(3000);console.log('Express app started on port 3000');No newline at end of fileNo newline at end of file