因此,以下代码段显示了如何设置RedisStore
并从传入的GET请求中获取sessionID
。但是,sessionStore.get( req.cookies['connect.sid'] )
的评估结果为undefined
。
有人可以指出我做错了什么吗?从sessionID
返回的示例req.cookies['connect.sid']
如下所示:
s:_8pvC4Zju4GjMOvN6Y4ej8SRVYvopn8C.ctuA/+pQnZt96bLgOW1xizHOLI2nBuH4vzO9Agm26Lk
var expressSession = require('express-session');
var RedisStore = require('connect-redis')(expressSession);
var sessionStore = new RedisStore({host: '127.0.0.1', port: 6379});
app.use(expressSession({store: sessionStore,
secret: 'xxxx' }));
app.get("/*", function(req, res, next) {
if(typeof req.cookies['connect.sid'] !== 'undefined') {
console.log('sessionID: ', req.cookies['connect.sid']);
console.log( sessionStore.get(req.cookies['connect.sid'] ));
}
next();
});
答案 0 :(得分:1)
sessionStore.get是一个异步方法,您需要将回调传递给此方法,如下所示:
sessionStore.get(req.cookies['connect.sid'], function (err, session) {
console.log(session);
})
观看有关express-session的文档。