Skip to content

Instantly share code, notes, and snippets.

@guileen
Created April 20, 2011 23:55
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save guileen/933376 to your computer and use it in GitHub Desktop.
Save guileen/933376 to your computer and use it in GitHub Desktop.
Express Custom 404
var express = require('express'),
mongoose = require('mongoose'),
app;
app = module.exports = express.createServer();
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ secret: 'meeper' }));
app.use(require('stylus').middleware({ src: __dirname + '/public' }));
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function() { app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); });
app.configure('production', function() { app.use(express.errorHandler()); });
app.error(function(err, req, res, next) {
if (err instanceof NotFound) {
res.render('404', { status: 404 });
} else {
res.render('500', { status: 500 });
}
});
function NotFound(msg) {
this.name = 'NotFound';
Error.call(this, msg);
Error.captureStackTrace(this, arguments.callee);
}
NotFound.prototype.__proto__ = Error.prototype;
// Routes
app.get('/', function(req, res) {
res.render('index', {
title: 'Express'
});
});
app.get('/*', function(req, res) {
throw new NotFound('Page not found.');
});
// Listen
// Only listen on $ node app.js
if (!module.parent) {
app.listen(3000);
console.log("Express server listening on port %d", app.address().port);
}
@webstyle
Copy link

webstyle commented Nov 5, 2016

Hi, thanks for example. Why you are use prototypes ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment