node.js + express + mongodb anyone ever tried to use express' session management with replica sets

StackOverflow https://stackoverflow.com/questions/7351318

i'm using

  • node 0.4.11
  • express 2.4.6
  • mongodb 1.8.3
  • mongoose 2.1.2
  • connect-mongodb 1.0.0

and trying to implement replica sets with authentication.

  1. i want to store different kind of application-data in the DB
  2. i want to store express' session-data in the DB

a "normal" connection with mongoose is working:

mongo.connectSet('mongodb://user:secret@host:27017/test, 
                  mongodb://host:27018, 
                  mongodb://host:27019, 
                  mongodb://host:27020', function (err) {
    if (err) {
        console.log("could not connect to DB: " + err);
    }
});

but how can get the session management to work?!

app.use(express.session({
            cookie: {maxAge: 60000 * 20}, // 20 minutes
            secret: 'foo',
            store: new MongoStore({
                host: 'host',
                port: [27017, 27018, 27019, 27020],
                dbname: 'test',
                rs_name: 'rstest',
                username: 'user',
                password: 'secret'
            })
          }));

this is not working :(

is this actually possible? or should i use a different mongodb for storing the session data?

有帮助吗?

解决方案

Are you sure you are using connect-mongodb and not connect-mongo?

I don't see where those parameters can be used with connect-mongodb.

Don't pass in the connection settings. connect-mongodb can take a direct db variable instead, which is an instance of mongodb.Db.

That means you can use the same connection that mongoose uses, instead of having connect-mongodb create a new connection just for sessions.

For mongoose, the mongodb.Db instance can be found at mongoose.connection.db.

So using your code as an example (assuming mongo is your mongoose object):

app.use(express.session({
            cookie: {maxAge: 60000 * 20}, // 20 minutes
            secret: 'foo',
            store: new MongoStore({db: mongo.connection.db})
          }));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top