Skip to content

Commit

Permalink
Upgrade connect-mongo to v0.5.x
Browse files Browse the repository at this point in the history
v0.5.x should resolve jdesboeufs/connect-mongo#80, see discussion there
for details.
  • Loading branch information
nylen committed Jan 4, 2015
1 parent b326822 commit 2f42828
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 74 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -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",
Expand Down
132 changes: 59 additions & 73 deletions server.js
Expand Up @@ -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);
});
}
});

0 comments on commit 2f42828

Please sign in to comment.