I am a beginner working with Express 4, MongoDB (native module) and express-session. In the past few months sessions have been persisting normally. But yesterday I attempted to make the server utilize connection pools by keeping the connection to the MongoDB database open. I was able to successfully do that, but in the process I broke the express-session functionality. Sessions do not persist across requests as they did before, but are dumped at the end of every request. I do not get any error messages or feedback. So my question is this: what could be causing this and why?
app.js (main application file, condensed for clarity to show only related code):
var mongoclient = new MongoClient(new Server("localhost", 27017),
{native_parser: true, auto_reconnect: true, poolSize: 10});
// Make connections reusable, allow server to
//continue to boot once connection has been established
MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, dbclient) {
// dbclient instance exported to routes module for use
// imports modules
var express = require('express'),
app = express();
var cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
session = require('express-session'),
routes = require('./routes/routes'),
analytics = require('./helpers/analytics');
...
// Configure Server;
app.use(cookieParser());
app.use(bodyParser());
app.use(bodyParser.json());
app.use(session({ secret: 'SECRET' }));
route definitions...
// Server configuration
var server = app.listen(3000, function() {
console.log("Listening on port %d", server.address().port);
});
});
Example route that is not persisting session (again, condensed for clarity):
exports.setUserInfo = function(request, response, next) {
var newUser = {...}
analytics.createItem(item, function(err, item) {
request.session.item = {
_id: item[0]._id,
date: item[0].date,
}
}
When I attempt to use this route I am able to tell that request.session.item is set within the route, however when I refresh the route, request.session.item is no longer set.
I understand that my work is crude, particular my establishing a single connection to the MongoDB database. If I can find a cleaner way to to that I will change the example to reflect it. If you need to see more code I am happy to provide it. Thank you for your help.