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 fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
// Expose modules in ./support for demo purposesrequire.paths.unshift(__dirname + '/../../support');
/** * Module dependencies. */
var express = require('../../lib/express');
var app = express.createServer();
// Serve default connect faviconapp.use(express.favicon());
// Logger is placed below favicon, so favicon.ico// requests will not be loggedapp.use(express.logger({ format: '":method :url" :status' }));
// "app.router" positions our routes // specifically above the middleware// assigned below
app.use(app.router);
// When no more middleware require execution, aka// our router is finished and did not respond, we// can assume that it is "not found". Instead of// letting Connect deal with this, we define our// custom middleware here to simply pass a NotFound// exception
app.use(function(req, res, next){ 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);}
/** * 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.
// These handlers could potentially be defined within// configure() blocks to provide introspection when// in the development environment.
app.error(function(err, req, res, next){ if (err instanceof NotFound) { res.render('404.jade', { status: 404, error: err }); } else { 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, error: err });});
// Routes
app.get('/', function(req, res){ res.render('index.jade');});
app.get('/404', function(req, res){ throw new NotFound(req.url);});
app.get('/500', function(req, res, next){ next(new Error('keyboard cat!'));});
app.listen(3000);console.log('Express app started on port 3000');