我正在使用 Expresss.js 的cookie-session模块来处理会话。
我希望在每次页面加载(或 ajax 调用)时更新会话。这就是他们通常在任何地方工作的方式。
文档只字未提。
我试过这样做,但没有成功:
app.use(function(req,res,next ){
req.sessionOptions.maxAge = 20*1000;
return next();
});
我正在使用 Expresss.js 的cookie-session模块来处理会话。
我希望在每次页面加载(或 ajax 调用)时更新会话。这就是他们通常在任何地方工作的方式。
文档只字未提。
我试过这样做,但没有成功:
app.use(function(req,res,next ){
req.sessionOptions.maxAge = 20*1000;
return next();
});
我怀疑您没有将响应 cookie 发送给客户端。我用一个中间件解决了同样的问题(使用 express 和 cookie-session),该中间件为每个 get 发送一个包含不同值的 cookie:
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 不会设置 Set-Cookie 标头来将响应 cookie 发送到客户端。