node.js 如何在 ExpressJS 中结束会话

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5573256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:00:13  来源:igfitidea点击:

How to end a session in ExpressJS

sessionnode.jsexpress

提问by Stephen

I feel like this has to be buried somewhere in the documentation, but I can't find it.

我觉得这必须埋在文档中的某个地方,但我找不到它。

How do you close or end or kill (whatever) a session in ExpressJS?

你如何在 ExpressJS 中关闭、结束或终止(无论如何)一个会话?

回答by Brad

Express 4.x Updated Answer

Express 4.x 更新答案

Session handling is no longer built into Express. This answer refers to the standard session module: https://github.com/expressjs/session

会话处理不再内置于 Express 中。这个答案指的是标准会话模块:https: //github.com/expressjs/session

To clear the session data, simply use:

要清除会话数据,只需使用:

req.session.destroy();

The documentation is a bit useless on this. It says:

文档在这方面有点无用。它说:

Destroys the session, removing req.session, will be re-generated next request. req.session.destroy(function(err) { // cannot access session here })

销毁会话,删除req.session,下次请求会重新生成。 req.session.destroy(function(err) { // cannot access session here })

This does notmean that the current session will be re-loaded on the next request. It means that a clean empty session will be created in your session store on next request. (Presumably the session ID isn't changing, but I have not tested that.)

但这并不意味着,本届会议将是下一个请求重新加载。这意味着在下次请求时将在您的会话存储中创建一个干净的空会话。(大概会话 ID 没有改变,但我没有测试过。)

回答by Stephen

Never mind, it's req.session.destroy();

没关系,就是 req.session.destroy();

回答by Blueshirts

The question didn't clarify what type of session store was being used. Both answers seem to be correct.

这个问题没有说明正在使用什么类型的会话存储。两个答案似乎都是正确的。

For cookie based sessions:

对于基于 cookie 的会话:

From http://expressjs.com/api.html#cookieSession

来自http://expressjs.com/api.html#cookieSession

req.session = null // Deletes the cookie.

For Redis, etc based sessions:

对于基于 Redis 等的会话:

req.session.destroy // Deletes the session in the database.

回答by stream7

From http://expressjs.com/api.html#cookieSession

来自http://expressjs.com/api.html#cookieSession

To clear a cookie simply assign the session to null before responding:

要清除 cookie,只需在响应之前将会话分配为 null:

req.session = null

回答by Nithin

use,

用,

delete req.session.yoursessionname;

回答by tfmontague

Using req.session = null;, won't actually delete the session instance. The most proper solution would be req.session.destroy();, but this is essentially a wrapper for delete req.session;.

使用req.session = null;, 实际上不会删除会话实例。最合适的解决方案是req.session.destroy();,但这本质上是delete req.session;.

https://github.com/expressjs/session/blob/master/session/session.js

https://github.com/expressjs/session/blob/master/session/session.js

Session.prototype.destroy = function(fn){
  delete this.req.session;
  this.req.sessionStore.destroy(this.id, fn);
  return this;
};

回答by Hasan Sefa Ozalp

Session.destroy(callback)

Session.destroy(回调)

Destroys the session and will unset the req.session property. Once complete, the callback will be invoked.

销毁会话并取消设置 req.session 属性。完成后,将调用回调。

Secure way↓ ?

安全方式↓ ?

req.session.destroy((err) => {
  res.redirect('/') // will always fire after session is destroyed
})


Unsecure way↓ ?

不安全方式↓ ?

req.logout();
res.redirect('/') // can be called before logout is done

回答by John Quasar

req.session.destroy(); 

The above did not work for me so I did this.

以上对我不起作用,所以我这样做了。

req.session.cookie.expires = new Date().getTime();

By setting the expiration of the cookie to the current time, the session expired on its own.

通过将 cookie 的过期时间设置为当前时间,会话自行过期。

回答by Gene Bo

As mentioned in several places, I'm also not able to get the req.session.destroy() function to work correctly.

正如在几个地方提到的,我也无法让 req.session.destroy() 函数正常工作。

This is my work around .. seems to do the trick, and still allows req.flash to be used

这是我的工作……似乎可以解决问题,并且仍然允许使用 req.flash

req.session = {};

If you delete or set req.session = null;, seems then you can't use req.flash

如果删除或设置req.session = null; ,看来你不能使用req.flash