Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added 404 and 5xx pages to mvc example

  • Loading branch information...
commit fa746cc027e37e1d521d8f237f775804311bcf76 1 parent 1e85178
@tj tj authored
View
23 examples/mvc/index.js
@@ -11,6 +11,9 @@ app.engine('html', require('ejs').renderFile);
// make ".html" the default
app.set('view engine', 'html');
+// set views for error and 404 pages
+app.set('views', __dirname + '/views');
+
// define a custom res.message() method
// which stores messages in the session
app.response.message = function(msg){
@@ -64,6 +67,26 @@ app.use(express.methodOverride());
// load controllers
require('./lib/boot')(app, { verbose: !module.parent });
+// assume "not found" in the error msgs
+// is a 404. this is somewhat silly, but
+// valid, you can do whatever you like, set
+// properties, use instanceof etc.
+app.use(function(err, req, res, next){
+ // treat as 404
+ if (~err.message.indexOf('not found')) return next();
+
+ // log it
+ console.error(err.stack);
+
+ // error page
+ res.status(500).render('5xx');
+});
+
+// assume 404 since no middleware responded
+app.use(function(req, res, next){
+ res.status(404).render('404', { url: req.originalUrl });
+});
+
if (!module.parent) {
app.listen(3000);
console.log('\n listening on port 3000\n');
View
3  examples/mvc/views/404.html
@@ -0,0 +1,3 @@
+<link rel="stylesheet" href="/style.css" />
+<h1>404: Not Found</h1>
+<p>Sorry we can't find <%= url %></p>
View
3  examples/mvc/views/5xx.html
@@ -0,0 +1,3 @@
+<link rel="stylesheet" href="/style.css" />
+<h1>500: Internal Server Error</h1>
+<p>Looks like something blew up!</p>
Please sign in to comment.
Something went wrong with that request. Please try again.