Merge pull request #226 from lirantal/master

comment fixes, indentation and general cosmetic cleanup
This commit is contained in:
Lior Kesos
2014-01-14 14:09:10 -08:00
6 changed files with 74 additions and 56 deletions

View File

@@ -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') || {}
);

12
config/env/all.js vendored
View File

@@ -1,10 +1,16 @@
'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,
// The secret should be set to a non-guessable string that
// is used to compute a session hash
sessionSecret: 'MEAN',
// The name of the MongoDB collection to store sessions in
sessionCollection: 'sessions'
}

View File

@@ -12,79 +12,84 @@ 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'));
},
// Levels are specified in a range of 0 to 9, where-as 0 is
// no compression and 9 is best compression, but slowest
level: 9
}));
//Don't use logger for test env
if (process.env.NODE_ENV !== 'test') {
// Only use logger for development environment
if (process.env.NODE_ENV === 'development') {
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',
secret: config.sessionSecret,
store: new mongoStore({
db: db.connection.db,
collection: 'sessions'
collection: config.sessionCollection
})
}));
//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,

View File

@@ -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,

View File

@@ -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);

View File

@@ -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 <port>
// Start the app by listening on <port>
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;