Created
April 20, 2011 23:55
-
-
Save guileen/933376 to your computer and use it in GitHub Desktop.
Express Custom 404
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
Hi, thanks for example. Why you are use prototypes ?