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

查看:146
本文介绍了如何使用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天全站免登陆