我(几乎)成功地使用Node.js与Express和Redis来处理会话。Node.js - 会话不会持续通过res.redirect()
我遇到的问题是当我使用res.redirect()
时,会话不被保留。
这是我如何能看到它:
req.session.username = username.toString();
console.log(req.session);
res.redirect('/home');
的执行console.log()打印:
{ lastAccess: 1322579131762,
cookie:
{ path: '/',
httpOnly: true,
_expires: Tue, 29 Nov 2011 15:06:31 GMT,
originalMaxAge: 60000 },
username: 'admin' }
现在,这里是下面的代码:
app.get('/home', [app.requireLogin], function(req, res, next) {
// Not showing the rest as it's not even getting there
// Instead, here is what's interesting
app.requireLogin = function(req, res, next) {
console.log(req.session);
这console.log()打印出此:
{ lastAccess: 1322579131775,
cookie:
{ path: '/',
httpOnly: true,
_expires: Tue, 29 Nov 2011 15:06:31 GMT,
originalMaxAge: 60000 } }
显然,'username'对象已经消失。会议没有保留它,只是重建了一个新的会议。
我该如何解决这个问题?如果您需要任何信息,请不要犹豫。
这里是我设置会话管理代码:
app.configure(function() {
// Defines the view folder and engine used.
this.set('views', path.join(__dirname, 'views'));
this.set('view engine', 'ejs');
// Allow parsing form data
this.use(express.bodyParser());
// Allow parsing cookies from request headers
this.use(express.cookieParser());
// Session management
this.use(express.session({
// Private crypting key
secret: 'keyboard cat',
store: new RedisStore,
cookie: {
maxAge: 60000
}
}));
this.use(app.router);
});
这里是整个项目(我的意思是,部分吧),对要点:https://gist.github.com/c8ed0f2cc858942c4c3b(忽略的渲染视图属性)
您还可以粘贴包含Redis会话存储的代码吗?这里有一个超时选项,可能你意外地把它设置得太低了。 – alessioalex
增加:)它设置在60秒,我显然不使用60秒来写入凭据... –
我认为这是一个小项目,我认为最好将它发布到某处(gist,pastie)并给一条链接。我敢打赌,这个错误在某种程度上是不粘贴的。 (只是猜测) – alessioalex