Skip to content

Commit

Permalink
refactoring so that app routes can be maintained more easily from app…
Browse files Browse the repository at this point in the history
…/ directory (which makes more sense)
  • Loading branch information
lirantal committed Jan 12, 2014
1 parent ee63f17 commit 0afb2e6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 18 deletions.
16 changes: 16 additions & 0 deletions app/routes/articles.js
Original file line number Diff line number Diff line change
@@ -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);

};
9 changes: 9 additions & 0 deletions app/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

module.exports = function(app, passport, auth) {

// Home route
var index = require('../controllers/index');
app.get('/', index.render);

};
18 changes: 1 addition & 17 deletions config/routes.js → app/routes/users.js
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

};
17 changes: 16 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <port>
var port = process.env.PORT || config.port;
Expand Down

0 comments on commit 0afb2e6

Please sign in to comment.