From cd130b23978e12481e2cca37ed1960d14a86b391 Mon Sep 17 00:00:00 2001 From: Tom Spencer Date: Tue, 15 Apr 2014 15:39:57 +0100 Subject: [PATCH] 404.html file now being served correctly --- templates/express/controllers/index.js | 32 ++++++++++++++++++-------- templates/express/routes.js | 4 +++- 2 files changed, 26 insertions(+), 10 deletions(-) diff --git a/templates/express/controllers/index.js b/templates/express/controllers/index.js index 5b781787a..ad8d3c7a0 100644 --- a/templates/express/controllers/index.js +++ b/templates/express/controllers/index.js @@ -2,17 +2,12 @@ var path = require('path'); -/** - * Send partial, or 404 if it doesn't exist - */ -exports.partials = function(req, res) { +var tryPath = function(req, res, onPathNotFound) { var stripped = req.url.split('.')[0]; var requestedView = path.join('./', stripped); res.render(requestedView, function(err, html) { if(err) { - console.log("Error rendering partial '" + requestedView + "'\n", err); - res.status(404); - res.send(404); + onPathNotFound(err, requestedView); } else { res.send(html); } @@ -20,8 +15,27 @@ exports.partials = function(req, res) { }; /** - * Send our single page app + * Send partial, or 404 if it doesn't exist + */ +exports.partials = function(req, res) { + tryPath(req, res, function(err, requestedView) { + console.log("Error rendering partial '" + requestedView + "'\n", err); + res.status(404); + res.send(404); + }); +}; + +/** + * Try and send the HTML page + * If it doesn't exist, send the index page + * This allows single page app in HTML5 history mode */ exports.index = function(req, res) { - res.render('index'); + tryPath(req, res, function() { + // The HTML file isn't on the server. + // This is probably a HTML5 route in the Angular app, + // so send the index file and let Angular take over. + console.log('index taking over'); + res.render('index'); + }); }; diff --git a/templates/express/routes.js b/templates/express/routes.js index 08587fce4..b57852a35 100644 --- a/templates/express/routes.js +++ b/templates/express/routes.js @@ -33,9 +33,11 @@ module.exports = function(app) { res.send(404); }); - // All other routes to use Angular routing in app/scripts/app.js + // Partials get their own route app.route('/partials/*') .get(index.partials); + + // For everything else app.route('/*') .get(<% if(mongoPassportUser) { %> middleware.setUserCookie,<% } %> index.index); }; \ No newline at end of file