Express.js 的仅限会话 cookie
http://www.javascriptkit.com/javatutors/cookie.shtml
另一方面,仅限会话 cookie 手,将信息存储在 浏览器内存,可用于 浏览器会话的持续时间。 换句话说,里面存储的数据 会话 cookie 可以从 存储时间直到浏览器 关闭。从一个页面移动到另一个页面 在此期间不会擦除 数据。
如何使用 Express.js 实现此目的?
http://www.javascriptkit.com/javatutors/cookie.shtml
Session-only cookies, on the other
hand, stores information in the
browser memory, and is available for
the duration of the browser session.
In other words, the data stored inside
a session cookie is available from the
time of storage until the browser is
closed. Moving from page to page
during this time does not erase the
data.
How can I achieve this using Express.js?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
首先,该网站是一个可怕的地方。
现在回答问题。
会话实际上是什么:
Session ID
或简称SID
)与存储在服务器上的会话数据重新关联。Express.js 支持内置会话。
示例显示的内容:
安装 Redis 需要做很多工作,但也可以使用 Express.js 的内置内存存储:
这将简单地记录您访问该页面、关闭浏览器并重新打开的次数。计数仍然存在。
您可以找到有关
MemoryStore
选项的更多信息,例如会话的最长生命周期等。此处。First off, that website is a horrible place to go.
Now on to the question.
What sessions actually are:
Session ID
or shortSID
- with the session data stored on the server.Express.js has support for sessions built in.
What the example shows:
Installing Redis requires quite some work, but it's also possible to use Express.js's built-in memory store:
This will simply keep track of how many times you visited the page, closed your browser and re-opend. The counts will still be there.
You can find more on the options of the
MemoryStore
, like maximum life time of a session, etc. here.以下是我想要的(某种程度上)。当我关闭浏览器时,信息就消失了。
The following is what I wanted (sort of). When I close browser the information is gone.
以上适用于 IE8、Firefox 和 Chrome。
重要的部分是 maxAge:null
The above works on IE8, Firefox and Chrome.
The important piece is maxAge:null
这将设置会话 cookie。浏览器关闭时它将被删除!
This will set session cookie. On browser close it will be erased!
下面是 Alfred 的回答(使用 Express 的会话)的更新代码.js)。
Below is the updated code for Alfred's answer (session using Express.js).
我知道这是一个老问题,但我添加了一个答案,因为这里的所有答案似乎都已过时、存在安全缺陷或完全错误。
截至目前,express 默认使用
MemoryStore
,您不需要显式处理它。此外,截至目前,express-session 的 官方自述文件页面 有一个严厉警告:开始不使用
MemoryStore
作为生产的会话存储,引用:如果您想使用 MongoDBStore 进行会话存储,这里有一个使用
connect-mongodb-session
的简单解决方案:I know this is an old question but I'm adding an answer since all answers here seem to be either outdated, have security flaws or are just plain wrong.
As of now, express uses the
MemoryStore
by default, you don't need to explicitly handle that.Also, as of now, the express-session's official readme page has a stark warning at the beginning to not use
MemoryStore
as the session store for production, quoting:Here's a simple solution with
connect-mongodb-session
if you want to use MongoDBStore for session storage: