0

I have used this and it works but still, I am not clear that what resave and saveUninitialized are? I have read all the theories but still, I don't get it.

In the light of my code, how should I play with it so I understand the usage of these 2 properties?

Code:

var express= require('express')
var eApp= express();
var session= require('express-session');
var bodyparser= require('body-parser');

eApp.use(session({secret: 'hunainkey', resave: false, saveUninitialized: false}));
//eApp.use(cookieParser());

eApp.use(bodyparser.json());

eApp.get('/get/:user', (req, res) => 
{
    req.session.user= req.params.user; 
    res.send("Session set");
});

eApp.get('/', (req, res) => 
{
    res.send(req.session.user);
});

eApp.listen(1000, () => console.log('nodeapp5 is listening on port 1000')); 
jameel
  • 31
  • 4

1 Answers1

-1

Check express session option documentation for more information https://github.com/expressjs/session

saveUninitialized

Forces a session that is "uninitialized" to be saved to the store.

resave

Forces the session to be saved back to the session store, even if the session was never modified during the request.

User cfl also explains here https://stackoverflow.com/a/36688600/3151646

(Unmodified 'state' is different to uninitialized state)

resave: For any request made

  • Nothing in the session needs to change (no login etc).
  • Change of session required (logged in)

"Forces session to be saved even when unmodified"

saveUninitialized: Is about the state of the session, if its still in the uninitialized state.

  • Not modified only, nothing in the session needs to change (no login etc).

"Forces a session that is "uninitialized" to be saved to the store. A session is uninitialized when it is new but not modified."

Community
  • 1
  • 1
Kwaadjei
  • 177
  • 4
  • 15