From 0afb2e6ec93b251730d5a7a55b98053c4415ab5b Mon Sep 17 00:00:00 2001 From: Liran Tal Date: Sun, 12 Jan 2014 23:55:34 +0200 Subject: [PATCH] refactoring so that app routes can be maintained more easily from app/ directory (which makes more sense) --- app/routes/articles.js | 16 ++++++++++++++++ app/routes/index.js | 9 +++++++++ config/routes.js => app/routes/users.js | 18 +----------------- server.js | 17 ++++++++++++++++- 4 files changed, 42 insertions(+), 18 deletions(-) create mode 100644 app/routes/articles.js create mode 100644 app/routes/index.js rename config/routes.js => app/routes/users.js (73%) mode change 100755 => 100644 diff --git a/app/routes/articles.js b/app/routes/articles.js new file mode 100644 index 0000000000..7464075821 --- /dev/null +++ b/app/routes/articles.js @@ -0,0 +1,16 @@ +'use strict'; + +module.exports = function(app, passport, auth) { + + // Article Routes + var articles = require('../controllers/articles'); + app.get('/articles', articles.all); + app.post('/articles', auth.requiresLogin, articles.create); + app.get('/articles/:articleId', articles.show); + app.put('/articles/:articleId', auth.requiresLogin, auth.article.hasAuthorization, articles.update); + app.del('/articles/:articleId', auth.requiresLogin, auth.article.hasAuthorization, articles.destroy); + + // Finish with setting up the articleId param + app.param('articleId', articles.article); + +}; diff --git a/app/routes/index.js b/app/routes/index.js new file mode 100644 index 0000000000..f0e8c23e62 --- /dev/null +++ b/app/routes/index.js @@ -0,0 +1,9 @@ +'use strict'; + +module.exports = function(app, passport, auth) { + + // Home route + var index = require('../controllers/index'); + app.get('/', index.render); + +}; diff --git a/config/routes.js b/app/routes/users.js old mode 100755 new mode 100644 similarity index 73% rename from config/routes.js rename to app/routes/users.js index 4a3e6f52ba..27fa716773 --- a/config/routes.js +++ b/app/routes/users.js @@ -3,7 +3,7 @@ module.exports = function(app, passport, auth) { // User Routes - var users = require('../app/controllers/users'); + var users = require('../controllers/users'); app.get('/signin', users.signin); app.get('/signup', users.signup); app.get('/signout', users.signout); @@ -62,20 +62,4 @@ module.exports = function(app, passport, auth) { failureRedirect: '/signin' }), users.authCallback); - - // Article Routes - var articles = require('../app/controllers/articles'); - app.get('/articles', articles.all); - app.post('/articles', auth.requiresLogin, articles.create); - app.get('/articles/:articleId', articles.show); - app.put('/articles/:articleId', auth.requiresLogin, auth.article.hasAuthorization, articles.update); - app.del('/articles/:articleId', auth.requiresLogin, auth.article.hasAuthorization, articles.destroy); - - // Finish with setting up the articleId param - app.param('articleId', articles.article); - - // Home route - var index = require('../app/controllers/index'); - app.get('/', index.render); - }; diff --git a/server.js b/server.js index 1cb926fe12..7a78867d57 100755 --- a/server.js +++ b/server.js @@ -51,7 +51,22 @@ var app = express(); require('./config/express')(app, passport, db); // Bootstrap routes -require('./config/routes')(app, passport, auth); +var routes_path = __dirname + '/app/routes'; +var walk = function(path) { + fs.readdirSync(path).forEach(function(file) { + var newPath = path + '/' + file; + var stat = fs.statSync(newPath); + if (stat.isFile()) { + if (/(.*)\.(js$|coffee$)/.test(file)) { + require(newPath)(app, passport, auth); + } + } else if (stat.isDirectory()) { + walk(newPath); + } + }); +}; +walk(routes_path); + // Start the app by listening on var port = process.env.PORT || config.port;