Aniketh - The session needs to be stored in order to persist, which you already seem to have part of. For MongoDB, I use:
npm i connect-mongodb-session --save
const mongoose = require('mongoose');
const session = require('express-session');
const MongoDBStore = require('connect-mongodb-session')(session);
const MONGODB_URI = 'mongodb://localhost:27017/[database_name]';
const app = express();
const store = new MongoDBStore({
uri: MONGODB_URI,
collection: 'sessions'
})
// define database via mongoose
mongoose.connect(MONGODB_URI, {
useNewUrlParser: true
});
mongoose.set('useCreateIndex', true);
// connect to mongodb
const conn = mongoose.connection;
conn.on('error', console.error.bind(console, '[!] Connection Error '));
// session
app.use(
session({
secret: 'my secret',
resave: false,
saveUninitialized: false,
store: store
})
);
Now, when you check the database, you'll notice db.collection.session.
I use Browsersync in my Gulp pipeline, and needed to learn how to retain the session along with that and nodemon. In my case, this was the solution.
Steven Ventimiglia
Creative Technologist & Sr. Front-End Developer