From fe50b418ae4d75e7e28c9d7610ae8224186defb0 Mon Sep 17 00:00:00 2001 From: Amos Haviv Date: Sun, 29 Sep 2013 16:00:23 +0300 Subject: [PATCH] Fixing models auto loading bug --- app/controllers/articles.js | 7 +++---- config/config.js | 4 ++-- config/middlewares/authorization.js | 4 ++-- config/routes.js | 2 +- server.js | 23 ++++++++++++----------- 5 files changed, 20 insertions(+), 20 deletions(-) diff --git a/app/controllers/articles.js b/app/controllers/articles.js index 6c1098233a..037ba33ad1 100644 --- a/app/controllers/articles.js +++ b/app/controllers/articles.js @@ -22,7 +22,7 @@ exports.article = function(req, res, next, id) { /** * Create a article */ -exports.create = function(req, res) { +exports.create = function(req, res) { var article = new Article(req.body); article.user = req.user; @@ -32,8 +32,7 @@ exports.create = function(req, res) { errors: err.errors, article: article }); - } - else { + } else { res.jsonp(article); } }); @@ -89,4 +88,4 @@ exports.all = function(req, res) { res.jsonp(articles); } }); -}; +}; \ No newline at end of file diff --git a/config/config.js b/config/config.js index 18f307ff18..69504d5ad0 100644 --- a/config/config.js +++ b/config/config.js @@ -3,5 +3,5 @@ var _ = require('underscore'); // Load app configuration module.exports = _.extend( - require(__dirname + '/../config/env/all.js'), - require(__dirname + '/../config/env/' + process.env.NODE_ENV + '.json') || {}) ; + require(__dirname + '/../config/env/all.js'), + require(__dirname + '/../config/env/' + process.env.NODE_ENV + '.json') || {}); \ No newline at end of file diff --git a/config/middlewares/authorization.js b/config/middlewares/authorization.js index d5f236c557..9732488c0d 100755 --- a/config/middlewares/authorization.js +++ b/config/middlewares/authorization.js @@ -5,7 +5,7 @@ exports.requiresLogin = function(req, res, next) { if (!req.isAuthenticated()) { return res.send(401, 'User is not authorized'); } - next(); + next(); }; /** @@ -30,4 +30,4 @@ exports.article = { } next(); } -}; +}; \ No newline at end of file diff --git a/config/routes.js b/config/routes.js index 0092e50a0c..b793afdf79 100755 --- a/config/routes.js +++ b/config/routes.js @@ -9,7 +9,7 @@ module.exports = function(app, passport, auth) { //Setting up the users api app.post('/users', users.create); - + app.post('/users/session', passport.authenticate('local', { failureRedirect: '/signin', failureFlash: 'Invalid email or password.' diff --git a/server.js b/server.js index f4b94b6988..10f7cff57a 100755 --- a/server.js +++ b/server.js @@ -23,17 +23,18 @@ var db = mongoose.connect(config.db); //Bootstrap models var models_path = __dirname + '/app/models'; -var walk = function (path) { - fs.readdirSync(path).forEach(function (file) { - var newPath = path + '/' + file; - var stat = fs.statSync(newPath); - if (stat.isFile()) { - require(newPath); - } - else if (stat.isDirectory()) { - walk(newPath); - } - }); +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); + } + } else if (stat.isDirectory()) { + walk(newPath); + } + }); }; walk(models_path);