Node.js - 会话不会通过res.redirect()持久化 [英] Node.js - Session doesn't persist through res.redirect()

查看:178
本文介绍了Node.js - 会话不会通过res.redirect()持久化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在(几乎)使用Express.js与Express和Redis来成功地使用Node.js来处理会话。我遇到的问题是会话不被保留当我使用 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 2011年11月15:06:31 GMT,
originalMaxAge:60000},
用户名:'admin'}

现在,这里是以下代码:

  app.get('/ home',[app .requireLogin],function(req,res,next){
//没有显示其余的,因为它甚至没有到达
//相反,这里是有趣的
app.requireLogin = function (req,res,next){
console.log(req.session);

这个console.log()打印出来:

  {lastAccess:1322579131775,
cookie:
{路径:'/',
httpOnly:true,
_expires:星期二,2011年11月29日15:06:31 GMT,
originalMaxAge:60000}}

显然,'用户名'obj ect已经消失,会议没有保留,只是重建了一个新的。



我该如何解决?如果您需要任何信息,请不要犹豫。



以下是我设置会话管理的代码:

  app.configure(function(){
//定义使用的视图文件夹和引擎
this.set('views',path.join(__ dirname, 'views'));
this.set('view engine','ejs');

//允许解析表单数据
this.use(express.bodyParser ));

//允许从请求标头解析cookie
this.use(express.cookieParser());
//会话管理
this.use( express.session({
//私人密码
secret:'keyboard cat',
store:new RedisStore,
cookie:{
maxAge:60000
}
}));
this.use(app.router);
});

这是整个项目(我的意思是它的一部分),关于gist:https://gist.github.com/c8ed0f2cc858942c4c3b (忽略呈现的视图的属性)

解决方案

好的,我找到了解决方案。问题是, maxAge 中的时间已添加到当前日期。所以,在浏览器端,cookie被设置为在GMT时间显示过期。



问题是如下:我使用虚拟机来测试node.js ,而且你知道...有时你暂停你的机器。



嗯,发生了什么事情是机器的时间迟了两天。所以,每当Cookie在服务器端设置时,客户端都认为cookie已经过期,因为我的主机不是两天迟。



另一个愚蠢的结果。


I'm (almost) successfully using Node.js with Express and Redis to handle sessions.

The problem I'm having is that the session is not kept when I use res.redirect().

Here is how I can see it :

req.session.username = username.toString();
console.log(req.session);
res.redirect('/home');

The console.log() prints :

{ lastAccess: 1322579131762,
  cookie:
   { path: '/',
     httpOnly: true,
     _expires: Tue, 29 Nov 2011 15:06:31 GMT,
     originalMaxAge: 60000 },
  username: 'admin' }

Now, here is the following code :

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);

This console.log() prints out this :

{ lastAccess: 1322579131775,
  cookie:
   { path: '/',
     httpOnly: true,
     _expires: Tue, 29 Nov 2011 15:06:31 GMT,
     originalMaxAge: 60000 } }

Clearly, the 'username' object has disappeared. The session has not kept it, and just rebuilt a new one.

How can I solve this? Don't hesitate if you need any information.

Here is the code where I set the session management :

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);
});

Here is the whole project (I mean, parts of it), on gist : https://gist.github.com/c8ed0f2cc858942c4c3b (ignore the properties of the rendered views)

解决方案

Alright, I found the solution. The problem is that the time in maxAge was added to the current date. So, in the browser side, the cookie was set to expire at the GMT time shown.

The problem was the following : I use a virtual machine to test node.js, and, you know... sometimes, you suspend your machine.

Well, what happened is that the machine's time was two days late. So, whenever the cookie was set on the server side, the client side thought the cookie was already expired, since my host machine was not two days late.

Another stupid outcome.

这篇关于Node.js - 会话不会通过res.redirect()持久化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆