使用maxAge与Express.js的正确方法是什么? [英] What's the correct way to use maxAge with Express.js?

查看:262
本文介绍了使用maxAge与Express.js的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了几个变化。假设我希望我的cookie在一秒钟后过期。我应该使用

  app.use(express.session({secret:'mysecret',maxAge:new Date(Date.now ()+ 1000)})); 

  app.use(express.session({secret:'mysecret',maxAge:1000})); 

  app.use(express.session({secret:'mysecret',cookie:{maxAge:new Date(Date.now()+ 1000)}})); 

  app.use(express.session({secret:'mysecret',cookie:{maxAge:1000}})); 

同时假设我已经将cookie正确设置,并且已过期。如果用户没有重新启动他们的浏览器,他们仍然保留cookie信息,直到他们这样做?

解决方案

我想你会最好直接看源头。



对于Express,它使用连接中间件,这里是会话代码 $ b

此外,连接站点的更多文档
http: //www.senchalabs.org/connect/session.html



所以你可以做

  app.use(express.session({secret:'mysecret',cookie:{expires:new Date(Date.now()+ 1000)}})); 

  app.use(express.session({secret:'mysecret',cookie:{maxAge:1000}})); 


I have seen several variations. Suppose I wish my cookie to expire after one second. Should I use

app.use(express.session({ secret: 'mysecret',  maxAge: new Date(Date.now() + 1000)}));

or

app.use(express.session({ secret: 'mysecret',  maxAge: 1000}));

or

app.use(express.session({ secret: 'mysecret',  cookie: {maxAge: new Date(Date.now() + 1000)}}));

or

app.use(express.session({ secret: 'mysecret',  cookie: {maxAge: 1000}}));

Also suppose I have set my cookie expiring correctly and it has expired. If the user does not restart their browser do they still retain the cookie information until they do so?

解决方案

I think you'll be better off looking at the source directly.

For Express it uses connect middleware, and here's the code for session https://github.com/senchalabs/connect/blob/master/lib/middleware/session.js#L67

And theres more documentation at the connect site http://www.senchalabs.org/connect/session.html

So you can do

app.use(express.session({ secret: 'mysecret',  cookie: {expires: new Date(Date.now() + 1000)}}));

or

app.use(express.session({ secret: 'mysecret',  cookie: {maxAge: 1000}}));

这篇关于使用maxAge与Express.js的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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