roccomuso / memorystore

express-session full featured MemoryStore layer without leaks!
MIT License
120 stars 20 forks source link

checkPeriod does not give any result #7

Closed ghost closed 6 years ago

ghost commented 6 years ago

I'm doing a site on nodejs and found your plugin, but it does not work correctly. Here is my code:

const session = require('express-session');
const MemoryStore = require('memorystore')(session);
app.use(session({
    secret: 'jdjdjdjdjdjd',
    resave: false,
    saveUninitialized: false,
    store: new MemoryStore({
      checkPeriod: 20000
    })
}));
app.get('/test', function(req, res) {
    req.session.count = req.session.count || 0;
    req.session.count++;
    res.send('sessionID: '+req.session.id
            +'<br>Visited: '+req.session.count);
});

By the code it is clear that cookies should expire after 20 seconds. But having checked the work, I found out that the cookies do not expire, they are stored endlessly and the result is always the same. What to do?

roccomuso commented 6 years ago

That is a "checkPeriod", but by default express-session sets a cookie max-age time to 24h, that will be used to check if an entry should be pruned or not.

You need to update your session configuration and add a:

cookie: { maxAge: 15000 } // cookie expires after 15sec.

The checkPeriod of memorystore will prune it once it's expired.

ghost commented 6 years ago

Ok