似乎无法弄清楚如何使用 redis 存储与 Express 4 进行会话,一些示例代码如下所示:
var express = require('express');
var app = express();
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');
var RedisStore = require('connect-redis')(session);
app.use(cookieParser());
app.use(session({
store: new RedisStore({
host: 'localhost',
port: 6379,
}),
secret: 'secret',
resave: false,
saveUninitialized: true
}));
app.get('/', function (req, res) {
console.log(req.session); // undefined! :(
});
app.listen(3000);
如您所见,上面的 req.session 似乎总是未定义,并且显然无法在其中存储任何变量...