Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions templates/express/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,40 @@

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);
}
});
};

/**
* 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');
});
};
4 changes: 3 additions & 1 deletion templates/express/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};