NodeJS - 没有表达的会话处理
NodeJS - Session handling WITHOUT express
我已经构建了我的应用程序,我在 NodeJS 上使用纯 JS,它是一个单页应用程序。我没用快递。
首先,用户需要登录。登录数据通过 websocket 发送到服务器,然后根据 MySql-DB 检查凭据。如果它们正确,则生成登录内容并将其发送回客户端并显示。
现在当用户已经登录,然后刷新浏览器,他登陆到应用程序的初始状态,需要重新登录。
我该如何解决这个问题?
我阅读了很多关于 NodeJS 中的会话处理的文章,但大多数文章都包含 express,这让我无法理解整个概念。
HTTP 本身是无状态的,因此您需要某种方式来识别用户。
传统上,这是通过 cookie 完成的。当您响应 HTTP 请求时,您会在响应中包含一个 cookie headers。对于所有后续的 HTTP 请求,客户端会将此 cookie 信息返回给您。
这意味着您可以发送某种 session 标识符,对于所有未来的请求,您可以查找 session 数据。对话有点像这样。
Client: Here's my login information, and I'd like the home page.
Server: Ok, thanks. Here's the home page. Also, remember that your session ID is 12345. Next time you ask me for something, tell me that session ID. (Logs in the database that session ID 12345 is associated with someuser.)
然后……
Client: I'd like this other page. You told me to tell you that my session ID is 12345.
Server: (Loads session information for 12345, sees that it's associated with someuser.) Ok, here's that other page.
您实际上如何存储所有内容取决于您。许多人使用数据库,因为他们通常已经在应用程序中使用它们,并且可以轻松地与应用程序服务器的多个实例共享 session 数据。
我已经构建了我的应用程序,我在 NodeJS 上使用纯 JS,它是一个单页应用程序。我没用快递。
首先,用户需要登录。登录数据通过 websocket 发送到服务器,然后根据 MySql-DB 检查凭据。如果它们正确,则生成登录内容并将其发送回客户端并显示。
现在当用户已经登录,然后刷新浏览器,他登陆到应用程序的初始状态,需要重新登录。
我该如何解决这个问题?
我阅读了很多关于 NodeJS 中的会话处理的文章,但大多数文章都包含 express,这让我无法理解整个概念。
HTTP 本身是无状态的,因此您需要某种方式来识别用户。
传统上,这是通过 cookie 完成的。当您响应 HTTP 请求时,您会在响应中包含一个 cookie headers。对于所有后续的 HTTP 请求,客户端会将此 cookie 信息返回给您。
这意味着您可以发送某种 session 标识符,对于所有未来的请求,您可以查找 session 数据。对话有点像这样。
Client: Here's my login information, and I'd like the home page.
Server: Ok, thanks. Here's the home page. Also, remember that your session ID is 12345. Next time you ask me for something, tell me that session ID. (Logs in the database that session ID 12345 is associated with someuser.)
然后……
Client: I'd like this other page. You told me to tell you that my session ID is 12345.
Server: (Loads session information for 12345, sees that it's associated with someuser.) Ok, here's that other page.
您实际上如何存储所有内容取决于您。许多人使用数据库,因为他们通常已经在应用程序中使用它们,并且可以轻松地与应用程序服务器的多个实例共享 session 数据。