I need to get the value of connect.sid. Everything works fine when this cookie is already installed - I can read it from req.headers.cookie . But at the first request, when the session is only initialized, it is not there. How do i get this value? Can you somehow make it from req.sessionID ? Here is the code where you can try:

 var express = require('express'), session = require('express-session'), cookieParser = require('cookie-parser'), app = express(); app.use(session({ cookie: { expires: new Date(Date.now() + 8*60*60*1000), maxAge: 8*60*60*1000, httpOnly: false, path: "/" }, secret: 'secret', saveUninitialized: true, unset: 'destroy', resave: true })); app.use(cookieParser('secret')); app.use('/', function(req, res){ console.log(req.headers.cookie, res.headers, req.cookies); //При первом запросе - undefined, undefined, {} try{ var t = req.headers.cookie.split('='); var c_sid = t[t.indexOf('connect.sid') + 1].split(';')[0]; console.log('connect.sid', c_sid); res.end(c_sid); }catch(e){ res.json(e); } }); app.listen(80); 

    1 answer 1

    Having rummaged in the express-session module code, I found how this connect.sid generated:

     var connectsid = 's.'+req.sessionID+'.'+crypto.createHmac('sha256', secret) .update(req.sessionID).digest('base64').replace(/\=+$/, ''); 

    Where secret is the secret that you specify in the session options.