5

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';
  1. Why these two are different?
  2. How can I get connect.sid before sending a response?
4

1 回答 1

3

我阅读了connect.session的源代码,得到:

key = options.key || 'connect.sid'

var val = 's:' + signature.sign(req.sessionID, secret);
val = cookie.serialize(key, val);
debug('set-cookie %s', val);
res.setHeader('Set-Cookie', val);

因此,当响应的 'header' 事件触发时,connect 会将 cookie 设置为响应头,并且当您调用 response.end() 时,connect 会将会话数据保存到存储中。

而已。

于 2013-06-05T07:27:20.093 回答