I'm trying to learn how express session is used. I wrote the following code. When I visit the /session route from Postman, every time it shows following in console:
not set
vikaskumar
What I expect is vikaskumar only, after first request. I guess it is saving this string in session but not storing in memory store. But as per docs, the memory store is default, so it should save it automatically?
Here's the code:
app.use(session({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true
}));
app.use(function (req, res, next) {
var views = req.session.username;
if (!views) {
console.log("not set");
req.session.username = 'vikaskumar';
}
next();
});
app.get("/session", function(req, res){
console.log(req.session.username);
res.write(req.session.username);
res.end();
});