如何使用SessionID从RedisStore(connect-redis)获取会话数据?

时间:2015-12-05 05:11:35

标签: node.js session express redis socket.io

因此,以下代码段显示了如何设置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();
});

1 个答案:

答案 0 :(得分:1)

sessionStore.get是一个异步方法,您需要将回调传递给此方法,如下所示:

sessionStore.get(req.cookies['connect.sid'], function (err, session) {
  console.log(session);
})

观看有关express-session的文档。