[Fixed] express persist session cookie across app instances

Issue

I am using the express-session middleware

const session = require('express-session');
app.use(session({
  secret: SECRET,
  resave: false,
  saveUninitialized: false
}));

accessing through req.session.

If my app crashes or is stopped, then all saved cookies become invalid/are forgotten by the server.

How can I make it so that previously valid cookies remain valid even after app restarts?

Solution

It’s not the cookie that disappears, but the server-side session object that the ID in the cookie points to. Please see the second paragraph in the documentation of express-session: https://github.com/expressjs/session

Warning The default server-side session storage, MemoryStore, is purposely not designed for a production environment. It will leak memory under most conditions, does not scale past a single process, and is meant for debugging and developing.

Follow the instructions in the README, pick a session store module from the compatible session stores list, and use it to persist your session state.

Leave a Reply

(*) Required, Your email will not be published