更新express.js的Cookie会话 [英] Renewing Cookie-session for express.js

查看:73
本文介绍了更新express.js的Cookie会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Expressa.js的 cookie-session 模块来处理会话.

I'm using the cookie-session module for Expresss.js to deal with sessions.

我希望每次页面加载(或ajax调用)时都更新会话.这就是它们通常在任何地方工作的方式.

I would like the sessions to be renewed on every page load (or ajax call). That's how they usually work anywhere.

文档中只字未提.

我尝试这样做,但是没有成功:

I've tried doing this, but without success:

app.use(function(req,res,next ){
     req.sessionOptions.maxAge = 20*1000;
     return next();
});

推荐答案

我怀疑您没有将响应cookie发送给客户端.我用中间件解决了相同的问题(使用express和cookie-session),该中间件发送的cookie每次获取都包含不同的值:

I suspect that you are not sending the response cookie to the client. I solved the same problem (using express and cookie-session) with a middleware that sends a cookie containing a different value for each get:

var cookieSession = require('cookie-session') 

app.use(cookieSession({
  key: cookieKey,
  secret: cookieSecret,
  maxAge: 1 * 60 * 60 * 1000 // 1 hour (rolling)
  })
);

app.get('*', function(req, res, next) {
  // To update the session expiration time we need to send the new
  // expiration in the response cookie.
  // To send again the response cookie to the client we need to
  // update the session object.
  req.session.fake = Date.now();
  next();
});

确实,如果会话对象不变,cookie-session v.1.2.0不会

Indeed, if the session object does not change, cookie-session v. 1.2.0 does not set the Set-Cookie header to send the response cookie to the client.

这篇关于更新express.js的Cookie会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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