Node/Express with connect-redis, how handle session expiry Node/Express with connect-redis, how handle session expiry express express

Node/Express with connect-redis, how handle session expiry


Using Redis with express-session, you can use the touch() method from express-session to reset the TTL. So if you set a TTL when creating the session, do something like this on the routes where you don't want the session to expire:

api.get("/someRoute/", (req, res) => {  req.session.touch();  // Whatever else you need to do  res.sendStatus(200);}

That will reset the TTL on Redis and prevent the session from expiring assuming the client is still hitting your API - I'm assuming that if the client doesn't interact with your API for long enough, that implies the browser is closed or otherwise finished using your app.


You can specify a ttl while creating the session store.You can find more options in the readme.

app.use(session({    store: new RedisStore(options),    secret: 'keyboard cat',    ttl : 20 // ttl is in seconds. From the readme.}));