node.jsmongodbexpresssession-storeconnect-mongo

callback not working in connect-mongo


i have used connect-mongo as session store . connect-mongo@0.4.0.

my code is

app.use(express.session({

    store: new MongoStore({
        url: 'mongodb://192.168.1.124:27017/ip_session',
    }, function(e) {
        // Start the app by listening on <port>
        var port = process.env.PORT || config.guiPort;

        var server = app.listen(port);
        console.log('Express app started on port ' + port);
    }),
    // 2 * 60 * 60 * 1000
    cookie: {
        expires: new Date(Date.now() + 7200000)
    },    
    secret: '**********'
}));

till that all working fine, but when i upgraded connect-mongo version to latest (0.8.1). callback that contains server starting script is not working.

i.e

function(e) {
    var port = process.env.PORT || config.guiPort;

    var server = app.listen(port);
    console.log('Express app started on port ' + port);
}

above code is not working after updating version of connect-mongo.

your help will be appreciable. thanks in advance.


Solution

  • Judging by the code, instances of MongoStore emit events when its (connection) state changes. So perhaps this works:

    var store = new MongoStore({ url : ... });
    
    store.on('connected', function() {
      // Start the app by listening on <port>
      var port = process.env.PORT || config.guiPort;
    
      var server = app.listen(port);
      console.log('Express app started on port ' + port);
    });
    
    app.use(express.session({
      store: store,
      ...
    });