Permalink
Browse files

Added error handling routes

  • Loading branch information...
1 parent 4537e8e commit 7f63082b053c6b4b703385de9279c258e3826d57 @robrighter committed Aug 18, 2010
Showing with 41 additions and 20 deletions.
  1. +41 −20 project/app.js
View
@@ -1,18 +1,36 @@
//setup Dependencies
require(__dirname + "/../lib/setup").ext( __dirname + "/../lib").ext( __dirname + "/../lib/express/support");
-var connect = require('connect');
-var sys = require('sys');
-var io = require('socket.io-node');
+var connect = require('connect')
+ , express = require('express')
+ , sys = require('sys')
+ , io = require('socket.io-node')
+ , port = 8081;
//Setup Express
-var server = require('express').createServer();
+var server = express.createServer();
server.configure(function(){
server.set('views', __dirname + '/views');
server.use(connect.bodyDecoder());
server.use(server.router);
server.use(connect.staticProvider(__dirname + '/static'));
});
-var port = 8081;
+
+//setup the errors
+server.error(function(err, req, res, next){
+ if (err instanceof NotFound) {
+ res.render('404.ejs');
+ } else {
+ res.render('500.ejs', { locals: {
+ header: '#Header#'
+ ,footer: '#Footer#'
+ ,title : 'Page Title'
+ ,description: 'Page Description'
+ ,author: 'Your Name'
+ ,analyticssiteid: 'XXXXXXX'
+ ,error: err
+ } });
+ }
+});
server.listen( port);
//Setup Socket.IO
@@ -28,18 +46,13 @@ io.on('connection', function(client){
});
});
-console.log('Listening on http://0.0.0.0:' + port )
-//setup the errors
-server.error(function(err, req, res, next){
- if (err instanceof NotFound) {
- res.render('404.ejs');
- } else {
- res.render('500.ejs', { locals: { error: err } });
- }
-});
+///////////////////////////////////////////
+// Routes //
+///////////////////////////////////////////
+
+/////// ADD ALL YOUR ROUTES HERE /////////
-//Routes
server.get('/', function(req,res){
res.render('index.ejs', {
locals : {
@@ -53,14 +66,22 @@ server.get('/', function(req,res){
});
});
-//Test Routes for 404 and 500 errors
-server.get('/404', function(req, res){
- throw new NotFound;
-});
+//A Route for Creating a 500 Error (Useful to keep around)
server.get('/500', function(req, res){
- throw new Error('keyboard cat!');
+ throw new Error('This is a 500 Error');
+});
+
+//The 404 Route (ALWAYS Keep this as the last route)
+server.get('/*', function(req, res){
+ throw new NotFound;
});
+function NotFound(msg){
+ this.name = 'NotFound';
+ Error.call(this, msg);
+ Error.captureStackTrace(this, arguments.callee);
+}
+console.log('Listening on http://0.0.0.0:' + port );

0 comments on commit 7f63082

Please sign in to comment.