Question

I have the following code to set up a session store with connect-mong

 var express = require('express');
 var MongoStore = require('connect-mongo')(express);
 var app = express();

app.use(express.cookieParser());
app.use(express.session({
secret: '1234567890QWERTY',
maxAge: new Date(Date.now() + 3600000),
store: new MongoStore({
    db: 'mydb',
    host: '127.0.0.1'
}),
 cookie: { maxAge: 24 * 60 * 60 * 1000 }
}));

In the requests and responses from the server I can see the sessionIDs, but the database is not created or if I create it the session collection does not gets filled. I do not receive any errors on the console.

I cannot find what am I missing ( using express 3).

Était-ce utile?

La solution 2

I had multiple use of the session and cookieParser. This caused the problem.

Autres conseils

It happens because nobody connected yet to your express server. So there's no session to store.

Add

app.listen(3000, function() {
        console.log('now listening  on http://localhost:3000/'); 
});

At the end and connect to the server in your browser. Immediately you will get sessions collections in your Mongo db.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top