如何在ExpressJS中结束会话

问题描述:

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

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

您如何关闭或结束或终止ExpressJS中的会话(无论如何)?

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

Express 4.x更新的答案

会话处理不再内置于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:

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

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

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

This does not mean 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.)