Flo*_*ine 7 redis node.js express
我(几乎)成功地使用带有Express和Redis的Node.js来处理会话.
我遇到的问题是我使用时不保留会话res.redirect().
这是我如何看到它:
req.session.username = username.toString();
console.log(req.session);
res.redirect('/home');
Run Code Online (Sandbox Code Playgroud)
console.log()打印:
{ lastAccess: 1322579131762,
cookie:
{ path: '/',
httpOnly: true,
_expires: Tue, 29 Nov 2011 15:06:31 GMT,
originalMaxAge: 60000 },
username: 'admin' }
Run Code Online (Sandbox Code Playgroud)
现在,这是以下代码:
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);
Run Code Online (Sandbox Code Playgroud)
这个console.log()打印出来:
{ lastAccess: 1322579131775,
cookie:
{ path: '/',
httpOnly: true,
_expires: Tue, 29 Nov 2011 15:06:31 GMT,
originalMaxAge: 60000 } }
Run Code Online (Sandbox Code Playgroud)
显然,'用户名'对象已经消失.会议没有保留它,只是重建了一个新会议.
我怎么解决这个问题?如果您需要任何信息,请不要犹豫.
这是我设置会话管理的代码:
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);
});
Run Code Online (Sandbox Code Playgroud)
这是整个项目(我的意思是,它的一部分),依据是:https://gist.github.com/c8ed0f2cc858942c4c3b(忽略渲染视图的属性)
好吧,我找到了解决方案.问题是时间输入maxAge已添加到当前日期.因此,在浏览器端,cookie被设置为在显示的GMT时间到期.
问题如下:我使用虚拟机来测试node.js,你知道......有时,你暂停你的机器.
嗯,发生的事情是机器的时间晚了两天.因此,无论何时在服务器端设置cookie,客户端都认为cookie已经过期,因为我的主机迟了两天.
另一个愚蠢的结局.
| 归档时间: |
|
| 查看次数: |
7284 次 |
| 最近记录: |