var app = express.createServer(),
sys = require('sys');
// "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(msg){
function NotFound(path){
this.name = 'NotFound';
if (path) {
Error.call(this, 'Cannot find ' + path);
this.path = path;
} else {
Error.call(this, msg);
Error.call(this, 'Not Found');
}
Error.captureStackTrace(this, arguments.callee);
}
sys.inherits(NotFound, Error);
// in the development environment.
app.error(function(err, req, res, next){
if (err instanceof NotFound) {
res.render('404.jade');
res.render('404.jade', {
locals: {
error: err
}
});
} else {
next(err);
}
});