diff --git a/config/config.js b/config/config.js index b4b4230022..192d25d00b 100644 --- a/config/config.js +++ b/config/config.js @@ -1,9 +1,11 @@ 'use strict'; +// Utilize Lo-Dash utility library var _ = require('lodash'); -// Load app configuration - +// Extend the base configuration in all.js with environment +// specific configuration module.exports = _.extend( require(__dirname + '/../config/env/all.js'), - require(__dirname + '/../config/env/' + process.env.NODE_ENV + '.js') || {}); + require(__dirname + '/../config/env/' + process.env.NODE_ENV + '.js') || {} +); diff --git a/config/env/all.js b/config/env/all.js index 22d15f7e62..f662ec59e1 100644 --- a/config/env/all.js +++ b/config/env/all.js @@ -1,10 +1,10 @@ 'use strict'; -var path = require('path'), -rootPath = path.normalize(__dirname + '/../..'); +var path = require('path'); +var rootPath = path.normalize(__dirname + '/../..'); module.exports = { root: rootPath, port: process.env.PORT || 3000, - db: process.env.MONGOHQ_URL + db: process.env.MONGOHQ_URL } diff --git a/config/express.js b/config/express.js index 6bc2202730..d8560b6129 100755 --- a/config/express.js +++ b/config/express.js @@ -12,10 +12,11 @@ var express = require('express'), module.exports = function(app, passport, db) { app.set('showStackError', true); - //Prettify HTML + // Prettify HTML app.locals.pretty = true; - //Should be placed before express.static + // Should be placed before express.static + // To ensure that all assets and data are compressed (utilize bandwidth) app.use(express.compress({ filter: function(req, res) { return (/json|text|javascript|css/).test(res.getHeader('Content-Type')); @@ -23,28 +24,28 @@ module.exports = function(app, passport, db) { level: 9 })); - //Don't use logger for test env + // Don't use logger for test env if (process.env.NODE_ENV !== 'test') { app.use(express.logger('dev')); } - //Set views path, template engine and default layout + // Set views path, template engine and default layout app.set('views', config.root + '/app/views'); app.set('view engine', 'jade'); - //Enable jsonp + // Enable jsonp app.enable("jsonp callback"); app.configure(function() { - //cookieParser should be above session + // The cookieParser should be above session app.use(express.cookieParser()); - // request body parsing middleware should be above methodOverride + // Request body parsing middleware should be above methodOverride app.use(express.urlencoded()); app.use(express.json()); app.use(express.methodOverride()); - //express/mongo session storage + // Express/Mongo session storage app.use(express.session({ secret: 'MEAN', store: new mongoStore({ @@ -53,38 +54,40 @@ module.exports = function(app, passport, db) { }) })); - //connect flash for flash messages + // Connect flash for flash messages app.use(flash()); - //dynamic helpers + // Dynamic helpers app.use(helpers(config.app.name)); - //use passport session + // Use passport session app.use(passport.initialize()); app.use(passport.session()); - //routes should be at the last + // Routes should be at the last app.use(app.router); - //Setting the fav icon and static folder + // Setting the fav icon and static folder app.use(express.favicon()); app.use(express.static(config.root + '/public')); - //Assume "not found" in the error msgs is a 404. this is somewhat silly, but valid, you can do whatever you like, set properties, use instanceof etc. + // Assume "not found" in the error msgs is a 404. this is somewhat + // silly, but valid, you can do whatever you like, set properties, + // use instanceof etc. app.use(function(err, req, res, next) { - //Treat as 404 + // Treat as 404 if (~err.message.indexOf('not found')) return next(); - //Log it + // Log it console.error(err.stack); - //Error page + // Error page res.status(500).render('500', { error: err.stack }); }); - //Assume 404 since no middleware responded + // Assume 404 since no middleware responded app.use(function(req, res, next) { res.status(404).render('404', { url: req.originalUrl, diff --git a/config/passport.js b/config/passport.js index ef4eaa4dde..9814221b3c 100755 --- a/config/passport.js +++ b/config/passport.js @@ -11,11 +11,14 @@ var mongoose = require('mongoose'), module.exports = function(passport) { - //Serialize sessions + + // Serialize the user id to push into the session passport.serializeUser(function(user, done) { done(null, user.id); }); + // Deserialize the user object based on a pre-serialized token + // which is the user id passport.deserializeUser(function(id, done) { User.findOne({ _id: id @@ -24,7 +27,7 @@ module.exports = function(passport) { }); }); - //Use local strategy + // Use local strategy passport.use(new LocalStrategy({ usernameField: 'email', passwordField: 'password' @@ -51,7 +54,7 @@ module.exports = function(passport) { } )); - //Use twitter strategy + // Use twitter strategy passport.use(new TwitterStrategy({ consumerKey: config.twitter.clientID, consumerSecret: config.twitter.clientSecret, @@ -82,7 +85,7 @@ module.exports = function(passport) { } )); - //Use facebook strategy + // Use facebook strategy passport.use(new FacebookStrategy({ clientID: config.facebook.clientID, clientSecret: config.facebook.clientSecret, @@ -114,7 +117,7 @@ module.exports = function(passport) { } )); - //Use github strategy + // Use github strategy passport.use(new GitHubStrategy({ clientID: config.github.clientID, clientSecret: config.github.clientSecret, @@ -143,7 +146,7 @@ module.exports = function(passport) { } )); - //Use google strategy + // Use google strategy passport.use(new GoogleStrategy({ clientID: config.google.clientID, clientSecret: config.google.clientSecret, diff --git a/config/routes.js b/config/routes.js index 2456e868fe..4a3e6f52ba 100755 --- a/config/routes.js +++ b/config/routes.js @@ -1,23 +1,27 @@ 'use strict'; module.exports = function(app, passport, auth) { - //User Routes + + // User Routes var users = require('../app/controllers/users'); app.get('/signin', users.signin); app.get('/signup', users.signup); app.get('/signout', users.signout); app.get('/users/me', users.me); - //Setting up the users api + // Setting up the users api app.post('/users', users.create); - //Setting the local strategy route + // Setting up the userId param + app.param('userId', users.user); + + // Setting the local strategy route app.post('/users/session', passport.authenticate('local', { failureRedirect: '/signin', failureFlash: true }), users.session); - //Setting the facebook oauth routes + // Setting the facebook oauth routes app.get('/auth/facebook', passport.authenticate('facebook', { scope: ['email', 'user_about_me'], failureRedirect: '/signin' @@ -27,7 +31,7 @@ module.exports = function(app, passport, auth) { failureRedirect: '/signin' }), users.authCallback); - //Setting the github oauth routes + // Setting the github oauth routes app.get('/auth/github', passport.authenticate('github', { failureRedirect: '/signin' }), users.signin); @@ -36,7 +40,7 @@ module.exports = function(app, passport, auth) { failureRedirect: '/signin' }), users.authCallback); - //Setting the twitter oauth routes + // Setting the twitter oauth routes app.get('/auth/twitter', passport.authenticate('twitter', { failureRedirect: '/signin' }), users.signin); @@ -45,7 +49,7 @@ module.exports = function(app, passport, auth) { failureRedirect: '/signin' }), users.authCallback); - //Setting the google oauth routes + // Setting the google oauth routes app.get('/auth/google', passport.authenticate('google', { failureRedirect: '/signin', scope: [ @@ -58,10 +62,8 @@ module.exports = function(app, passport, auth) { failureRedirect: '/signin' }), users.authCallback); - //Finish with setting up the userId param - app.param('userId', users.user); - //Article Routes + // Article Routes var articles = require('../app/controllers/articles'); app.get('/articles', articles.all); app.post('/articles', auth.requiresLogin, articles.create); @@ -69,10 +71,10 @@ module.exports = function(app, passport, auth) { 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 + // Finish with setting up the articleId param app.param('articleId', articles.article); - //Home route + // Home route var index = require('../app/controllers/index'); app.get('/', index.render); diff --git a/server.js b/server.js index 39229cf9f6..1cb926fe12 100755 --- a/server.js +++ b/server.js @@ -13,19 +13,19 @@ var express = require('express'), * Please note that the order of loading is important. */ -//Load configurations -//Set the node enviornment variable if not set before +// Load configurations +// Set the node enviornment variable if not set before process.env.NODE_ENV = process.env.NODE_ENV || 'development'; -//Initializing system variables +// Initializing system variables var config = require('./config/config'), auth = require('./config/middlewares/authorization'), mongoose = require('mongoose'); -//Bootstrap db connection +// Bootstrap db connection var db = mongoose.connect(config.db); -//Bootstrap models +// Bootstrap models var models_path = __dirname + '/app/models'; var walk = function(path) { fs.readdirSync(path).forEach(function(file) { @@ -42,24 +42,24 @@ var walk = function(path) { }; walk(models_path); -//bootstrap passport config +// Bootstrap passport config require('./config/passport')(passport); var app = express(); -//express settings +// Express settings require('./config/express')(app, passport, db); -//Bootstrap routes +// Bootstrap routes require('./config/routes')(app, passport, auth); -//Start the app by listening on +// Start the app by listening on var port = process.env.PORT || config.port; app.listen(port); console.log('Express app started on port ' + port); -//Initializing logger +// Initializing logger logger.init(app, passport, mongoose); -//expose app +// Expose app exports = module.exports = app;