会话如何在使用Node.js的Express.js中工作? [英] How do sessions work in Express.js with Node.js?

查看:135
本文介绍了会话如何在使用Node.js的Express.js中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Express.js ,会话很简单。我很好奇他们实际工作方式。

Using Express.js, sessions are dead simple. I'm curious how they actually work though.

它是否在客户端存储一些cookie?如果是,我在哪里可以找到该cookie?如果需要,我该如何解码?

Does it store some cookie on the client? If so, where can I find that cookie? If required, how do I decode it?

我基本上希望能够看到用户是否登录,即使用户实际上不在网站上时间(像Facebook知道你在其他网站登录)。但我想明白,我应该先了解会话如何工作。

I basically want to be able to see if a user is logged in, even when the user is not actually on the site at the time (like how facebook knows you're logged in when you're on other sites). But I suppose to understand that I should first understand how sessions work.

推荐答案

我从来没有使用过Express.js, 关于其主题的文档

I have never used Express.js, although according to their documentation on the subject it sounds like:


  • Cookie存储在客户端上,使用一个密钥(服务器将使用该密钥检索会话数据)和一个散列(服务器将使用该密钥来确保Cookie数据没有因此如果您尝试更改值,则Cookie将无效)

  • Cookies are stored on the client, with a key (which the server will use to retrieve the session data) and a hash (which the server will use to make sure the cookie data hasn't been tampered with, so if you try and change a value the cookie will be invalid)

会话数据与某些框架相反(例如 Play Framework !)在服务器上保持,因此cookie比实际会话数据的持有者更像是会话的占位符。

The session data, as opposed to some frameworks (e.g. Play Framework!) is held on the server, so the cookie is more like a placeholder for the session than a holder of actual session data.

这里,服务器上的此会话数据默认保存在内存中,但可以更改为任何存储形式实现相应的API。

From here, it looks like this session data on the server is by default held in memory, although that could be altered to whatever storage form implements the appropriate API.

所以,如果你想检查没有特定 req 请求对象的事情,就像你说的,你只需要访问同一个存储。在第一个文档页面的底部,它详细描述了存储需要实现的所需方法,因此,如果您熟悉存储API,也许您可​​以执行 .getAll()如果存在类似的东西,并且循环访问会话数据并读取所需的值。

So if you want to check things without a specific req request object, like you said, you need to just access that same storage. On the bottom of the first documentation page, it details required methods the storage needs to implement, so if you're familiar with your storage API, maybe you could execute a .getAll() if something like that exists, and loop through the session data and read whatever values you want.

这篇关于会话如何在使用Node.js的Express.js中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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