CleanCrispCode CleanCrispCode - 7 months ago 46
Node.js Question

How to configure connect-mongo

I am using "connect-mongo" node module to store session data in mongodb.

Below is my code related to configuring connect-mongo

const cookieParser = require('cookie-parser');
const session = require('express-session');
const mongoStore = require('connect-mongo')(session);

secret: "test",
store: new mongoStore({
url: mongodbURL,
collection : 'sessions',
autoRemove: 'interval',
autoRemoveInterval: 1 // In minutes. Default

Once a user connects to root "/" url from browser, a record is getting inserted
in database

"_id": "EZ2sy6jHvnrlsyofqCrKVfPtp6hv5FX_",
"session": "{\"cookie\":{\"originalMaxAge\":null,\"expires\":null,\"httpOnly\":true,\"path\":\"/\"},\"name\":\"mongosession\"}",
"expires": ISODate("2016-06-14T15:19:15.548Z")

My questions are

  1. why is that originalMaxAge , expires keys of "session" are null and
    what needs to be modified so that these keys have a value.?

  2. Since I have set autoRemoveInterval to 1 min , I was assuming that session
    values from database will be removed in 1 min , but that was not the case
    What needs to be modified to achieve that ?

  1. Configure the cookie property of connect-session. By default, maxAge is null, making the cookie a (browser-)session cookie.
  2. Set the ttl property of connect-mongo, which defaults to 14 days (which looks about right, judging by the timestamp in the expires property). autoRemoveInterval only configures how often connect-mongo should check if there are any sessions that should expire.