Skip to content

Commit

Permalink
refact(server): move routes into their own library files
Browse files Browse the repository at this point in the history
  • Loading branch information
petebacondarwin committed Aug 17, 2013
1 parent e93b786 commit 3a7e9c5
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 30 deletions.
4 changes: 4 additions & 0 deletions server/lib/routes/admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exports.addRoutes = function (app, config) {
// users
// projects
};
7 changes: 7 additions & 0 deletions server/lib/routes/appFile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
exports.addRoutes = function(app, config) {
// This route deals enables HTML5Mode by forwarding missing files to the index.html
app.all('/*', function(req, res) {
// Just send the index.html for other files to support HTML5Mode
res.sendfile('index.html', { root: config.server.distFolder });
});
};
4 changes: 4 additions & 0 deletions server/lib/routes/projects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
exports.addRoutes = function (app, config) {
// projects
// scrums
};
19 changes: 19 additions & 0 deletions server/lib/routes/security.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
exports.addRoutes = function(app, security) {

app.post('/login', security.login);
app.post('/logout', security.logout);

// Retrieve the current user
app.get('/current-user', security.sendCurrentUser);

// Retrieve the current user only if they are authenticated
app.get('/authenticated-user', function(req, res) {
security.authenticationRequired(req, res, function() { security.sendCurrentUser(req, res); });
});

// Retrieve the current user only if they are admin
app.get('/admin-user', function(req, res) {
security.adminRequired(req, res, function() { security.sendCurrentUser(req, res); });
});

};
13 changes: 13 additions & 0 deletions server/lib/routes/static.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var express = require('express');

exports.addRoutes = function(app, config) {
// Serve up the favicon
app.use(express.favicon(config.server.distFolder + '/favicon.ico'));

// First looks for a static file: index.html, css, images, etc.
app.use(config.server.staticUrl, express.compress());
app.use(config.server.staticUrl, express.static(config.server.distFolder));
app.use(config.server.staticUrl, function(req, res, next) {
res.send(404); // If we get here then the request for a static file is invalid
});
};
33 changes: 3 additions & 30 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,7 @@ var app = express();
var secureServer = https.createServer(credentials, app);
var server = http.createServer(app);

// Serve up the favicon
app.use(express.favicon(config.server.distFolder + '/favicon.ico'));

// First looks for a static file: index.html, css, images, etc.
app.use(config.server.staticUrl, express.compress());
app.use(config.server.staticUrl, express['static'](config.server.distFolder));
app.use(config.server.staticUrl, function(req, res, next) {
res.send(404); // If we get here then the request for a static file is invalid
});
require('./lib/routes/static').addRoutes(app, config);

app.use(protectJSON);

Expand Down Expand Up @@ -68,27 +60,8 @@ app.namespace('/databases/:db/collections/:collection*', function() {
app.all('/', mongoProxy(config.mongo.dbUrl, config.mongo.apiKey));
});

app.post('/login', security.login);
app.post('/logout', security.logout);

// Retrieve the current user
app.get('/current-user', security.sendCurrentUser);

// Retrieve the current user only if they are authenticated
app.get('/authenticated-user', function(req, res) {
security.authenticationRequired(req, res, function() { security.sendCurrentUser(req, res); });
});

// Retrieve the current user only if they are admin
app.get('/admin-user', function(req, res) {
security.adminRequired(req, res, function() { security.sendCurrentUser(req, res); });
});

// This route deals enables HTML5Mode by forwarding missing files to the index.html
app.all('/*', function(req, res) {
// Just send the index.html for other files to support HTML5Mode
res.sendfile('index.html', { root: config.server.distFolder });
});
require('./lib/routes/security').addRoutes(app, security);
require('./lib/routes/appFile').addRoutes(app, config);

// A standard error handler - it picks up any left over errors and returns a nicely formatted server 500 error
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
Expand Down

0 comments on commit 3a7e9c5

Please sign in to comment.