Express 4 Sessions not persisting when restarting server Express 4 Sessions not persisting when restarting server express express

Express 4 Sessions not persisting when restarting server


The default session store for express-session is MemoryStore, which as the name suggests, stores sessions in memory only. If you need persistence, there are many session stores available for Express. Some examples:

For a updated and more complete list visit Compatible Session Stores.


@mscdex answer is great but in case you are looking for code samples. Here is one with connect-mongo which should work fine if you mongodb and mongoose.

Install the package:

npm i connect-mongo

require the package:

const session = require('express-session'); // You must have express-sessions installedconst MongoStore = require('connect-mongo')(session)

Now configure the session:

app.use(  session({    secret: "mysecrets",    resave: false,    saveUninitialized: false,    store: new MongoStore({       mongooseConnection: mongoose.connection,      ttl: 14 * 24 * 60 * 60     }),  }));

Again this assumes you are using mongoose and have the connection configured.

If you did everything right, it should work just fine.