Last active
August 29, 2015 13:57
-
-
Save Dexus/9840199 to your computer and use it in GitHub Desktop.
Error 404 ExpressJS without Error Function
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(function(req, res, next){ | |
res.status(404); | |
res.send('ERROR 404: Not Found'); | |
}); | |
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) { | |
res.render('500', { status: 500 }); | |
}); | |
// Routes | |
app.get('/', function(req, res) { | |
res.render('index', { | |
title: 'Express' | |
}); | |
}); | |
app.get('/*', function(req, res, next) { | |
next('route'); // ERROR 404: 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