I use express to support my session management:
app.use(express.cookieParser('your secret here'));
app.use(express.session({secret: 'mysecretcode'}));
I what to extract and save the sessionId from the request. However, the id I get from req.sessionID is different to that sent in the cookie:
req.sessionID --> 'E7oSoKmQfcMKnk5_jA5tF5vR'
cookie.connect.sid --> 's%3AE7oSoKmQfcMKnk5_jA5tF5vR.DQnYdDDcFn8K2JJHMgWL5DTzNYYwIU3DA5a10WImA7U';
I read the sourcecode of connect.session, and got:
key = options.key || 'connect.sid'
and
var val = 's:' + signature.sign(req.sessionID, secret);
val = cookie.serialize(key, val);
debug('set-cookie %s', val);
res.setHeader('Set-Cookie', val);
so, connect will set cookie into response header when response's 'header' event fired, and when you call response.end(), connect will save session data into store.
That's it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With