Skip to content

Instantly share code, notes, and snippets.

@kennx
Forked from guileen/app.js
Created February 18, 2017 08:23
Show Gist options
  • Save kennx/48b1bf9a2241693787f5e21c7775a3f8 to your computer and use it in GitHub Desktop.
Save kennx/48b1bf9a2241693787f5e21c7775a3f8 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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment