我该如何在会话到期之前立即进行操作(破坏) [英] How can I do something right before the session expire(destroy)

查看:58
本文介绍了我该如何在会话到期之前立即进行操作(破坏)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Web应用程序使用expressjs,并且使用了session中间件,我的问题是,在会话到期之前我该如何做些事情?

Hi I am using expressjs for my web application, and I use the session middleware, my question is that how I can do something right before the session expire?

例如,我在会话中存储了一些内容,并将req.session.cookie.maxAge设置为60000(1分钟),当会话超时时,我想将会话中的数据保存到磁盘上,我该怎么做?我检查了API,并且destroy函数仅支持callback ...

For example, I store something in the session, and I set the req.session.cookie.maxAge to 60000(1 minute), when the session timeout, I want to save the data in the session to disk, how I can do this? I checked the API and the destroy function only support callback...

谢谢!

推荐答案

假设您使用的是connect-session,则不会出现会话过期事件.

Assuming you're using connect-session there's nothing like session expiration event.

会话要么终止:

    客户端上的
  • (cookie过期时).在发出请求之前,您无法了解它,因此有可能永远不会触发该事件.
  • 会话存储中的
  • ,通常使用某种形式的生存时间.并非所有会话存储都支持事件,因此通常无法进行通知.
  • on the client (when cookie gets outdated). There's no way to know about it until a request is made so there's a danger that the event will never get triggered.
  • in the session store, usually using some form of time-to-live. Not all session stores support events, so notification can't be done in general.

但是,您可以尝试使用setTimeout()实施自己的检查-当超时时,请检查会话的到期日期是否足够接近现在,并执行所需的操作.

You can, however try implement your own check using setTimeout() - when it times out check if the session's expiration date is sufficiently close to now and do whatever you need to do.

这篇关于我该如何在会话到期之前立即进行操作(破坏)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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