From 2f4282889624a2ec017ce71600e9df7bca0052c9 Mon Sep 17 00:00:00 2001 From: James Nylen Date: Sat, 3 Jan 2015 18:46:23 -0600 Subject: [PATCH] Upgrade connect-mongo to v0.5.x v0.5.x should resolve kcbanner/connect-mongo#80, see discussion there for details. --- package.json | 2 +- server.js | 132 +++++++++++++++++++++++---------------------------- 2 files changed, 60 insertions(+), 74 deletions(-) diff --git a/package.json b/package.json index 2630703..2a5618f 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "dependencies" : { "config" : "~1.7.0", "connect-flash" : "~0.1.1", - "connect-mongo" : "~0.3.3", + "connect-mongo" : "~0.5.3", "express" : "~3.1.0", "express-namespace" : "~0.1.1", "js-yaml" : "~2.0.3", diff --git a/server.js b/server.js index f88b73c..c6ac185 100755 --- a/server.js +++ b/server.js @@ -138,94 +138,80 @@ app.ensureAuthenticated = function(req, res, next) { app.use(express.cookieParser()); -function listen(db, sessionStore) { - app.use(express.session({ - secret : config.app.secret, - store : sessionStore - })); +var sessionConfig = { + secret : config.app.secret +}; +if (config.app.mongoUrl) { + log.info('Using MongoDB session store'); + sessionConfig.store = new MongoStore({ + url : config.app.mongoUrl + }); +} else { + log.warn('MongoDB connection string (config.app.mongoUrl) not given'); + log.info('Using in-memory session store'); +} - app.use(flash()); - app.use(passport.initialize()); - app.use(passport.session()); +app.use(express.session(sessionConfig)); - var msgTypes = ['error', 'warning', 'info', 'success']; +app.use(flash()); +app.use(passport.initialize()); +app.use(passport.session()); - // Expose a way to define new middleware functions - this does not work - // after the app.use(app.router) statement. - app.middlewares = []; +var msgTypes = ['error', 'warning', 'info', 'success']; - // Custom middleware to set template variables - app.use(function(req, res, next) { - res.locals.namespace = namespace; - res.locals.qs = req.query; +// Expose a way to define new middleware functions - this does not work +// after the app.use(app.router) statement. +app.middlewares = []; - if (req.isAuthenticated()) { - res.locals.user = req.user; - } +// Custom middleware to set template variables +app.use(function(req, res, next) { + res.locals.namespace = namespace; + res.locals.qs = req.query; + + if (req.isAuthenticated()) { + res.locals.user = req.user; + } - res.locals.messages = []; - msgTypes.forEach(function(type) { - req.flash(type).forEach(function(msg) { - res.locals.messages.push({ - type : type, - message : msg - }); + res.locals.messages = []; + msgTypes.forEach(function(type) { + req.flash(type).forEach(function(msg) { + res.locals.messages.push({ + type : type, + message : msg }); }); + }); - function callMiddleware(i) { - if (i < app.middlewares.length) { - app.middlewares[i](req, res, function() { - callMiddleware(i + 1); - }); - } else { - next(); - } + function callMiddleware(i) { + if (i < app.middlewares.length) { + app.middlewares[i](req, res, function() { + callMiddleware(i + 1); + }); + } else { + next(); } + } - callMiddleware(0); - }); + callMiddleware(0); +}); - app.use(app.router); +app.use(app.router); - app.use(namespace, express.static(path.join(__dirname, 'public'))); +app.use(namespace, express.static(path.join(__dirname, 'public'))); - app.get('/', function(req, res) { - res.redirect(namespace); - }); +app.get('/', function(req, res) { + res.redirect(namespace); +}); - app.vars = { - namespace : namespace - }; +app.vars = { + namespace : namespace +}; - users.init(function() { - app.namespace(namespace, function() { - routes.setRoutes(app, config, function listen() { - app.listen(config.app.port); - log.info('Started server on port ' + config.app.port); - }); +users.init(function() { + app.namespace(namespace, function() { + routes.setRoutes(app, config, function listen() { + app.listen(config.app.port); + log.info('Started server on port ' + config.app.port); }); }); -} - -if (config.app.mongoUrl) { - MongoClient.connect(config.app.mongoUrl, function(err, db) { - if (err) { - log.warn('Error connecting to MongoDB: ' + (err.message || require('util').inspect(err))); - log.warn('Using in-memory session store'); - listen(null, null); - } else { - log.info('Using MongoDB session store'); - // Wait for store to initialize before allowing connections - // See https://github.com/kcbanner/connect-mongo/issues/80 - var store = new MongoStore({ db : db }, function() { - listen(db, store); - }); - } - }); -} else { - log.warn('MongoDB connection string (config.app.mongoUrl) not given'); - process.nextTick(function() { - listen(null, null); - }); -} +});