app.js
This file
Path
examples/pages/app.js
64 lines · 1.3 KiB · JavaScript
Last changed by
Merge branch 'fix-error'Tj Holowaychukbefore adoption498115d
History of this fileTake it
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
/** * Module dependencies. */
var express = require('./../../lib/express');
var app = express.createServer(), sys = require('sys');
app.set('views', __dirname + '/views');
// Provide our app with the notion of NotFound exceptions
function NotFound(msg){ this.name = 'NotFound'; Error.call(this, msg); Error.captureStackTrace(this, arguments.callee);}
sys.inherits(NotFound, Error);
// 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'); } 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', { locals: { error: err } });});
// Routes
app.get('/', function(req, res){ res.render('index.jade');});
app.get('/404', function(req, res){ throw new NotFound;});
app.get('/500', function(req, res){ throw new Error('keyboard cat!');});
app.listen(3000);