【问题标题】:Express Sessions and MySQL快速会话和 MySQL
【发布时间】:2020-09-07 07:47:28
【问题描述】:

我现在正试图将我的头脑围绕会话.. 目前,我有一个 express/NodeJS 应用程序,它使用查询参数来存储看起来像这样的脚本中的值

脚本 sn-p:

curl localhost:3000/register?name=bob\&width=13.970000\&time=0
curl localhost:3000/wheels?left=0.000000\&right=0.000000\&time=0 --cookie "USER=bob"
curl localhost:3000/echo?dist=9.220000\&time=10 --cookie "USER=bob"
curl localhost:3000/line?l1=1\&l2=1\&l3=1\&time=20 --cookie "USER=bob"
curl localhost:3000/other?ir=0\&time=30 --cookie "USER=bob"
curl localhost:3000/wheels?left=3.000000\&right=3.000000\&time=100 --cookie "USER=bob"
curl localhost:3000/echo?dist=9.220000\&time=110 --cookie "USER=bob"
curl localhost:3000/line?l1=1\&l2=1\&l3=1\&time=120 --cookie "USER=bob"
curl localhost:3000/other?ir=0\&time=130 --cookie "USER=bob"
curl localhost:3000/wheels?left=3.000000\&right=3.000000\&time=200 --cookie "USER=bob"
curl localhost:3000/echo?dist=9.220000\&time=210 --cookie "USER=bob"
curl localhost:3000/line?l1=1\&l2=1\&l3=1\&time=220 --cookie "USER=bob"

我已经能够将每条数据从名称、宽度、左、右、l1、l2 等存储到 MySQL 上的数据库中。我什至创建了一个不错的 UI 来浏览数据库并对其进行过滤。我项目的下一部分是现在创建一种浏览各种会话并查看它们是否处于活动状态的方法......但是,我在尝试这样做时遇到了问题。

首先,一些问题:

  1. 如何将这些会话存储到 mysql?
  2. 会话如何链接到用户存储在 数据库?例如,当我创建 UI 来浏览特定 会话,我希望能够单击会话,然后查看 他们存储的名称、宽度、左、右、l1、l2 等信息 当该用户登录时?

我已经阅读了 express-sessions 文档,但对于我正在寻找的数据库信息,它确实有点模糊。如果这没有意义,那么我很抱歉。我发现对我来说一半的战斗是将我的问题用语言表达出来,但我正在尽我最大的努力学习 express、node js 和 mysql。任何帮助将不胜感激!

【问题讨论】:

    标签: mysql node.js express session


    【解决方案1】:

    对于 mysql 的 Express-session,您可以使用像 https://www.npmjs.com/package/express-mysql-session 这样的连接器,并且作为给出的基本示例,您可以设置您的 express-session 将在 mysql 数据库中进行管理。

    当您在应用程序中使用 app.use(--) 进行配置时。因为它将是节点应用程序中的中间件。您将可以访问 req.session 对象

    简单用例 1.用户登录后,您有用户数据,您可以将该信息添加到req.session.user = {...userInfo}; 2.在访问req对象的其他调用中,设置会话后,您可以使用检索信息..const userInfo = req.session.user; 并且您希望 mysql 保存会话信息,因此按照设置会话 将对此进行管理。

    您可以访问诸如

    之类的方法
    req.session.destroy(function(err) {
      // cannot access session here -- you can use it at logout time or clear
    })
    

    【讨论】:

      猜你喜欢
      • 2012-09-27
      • 2016-08-08
      • 1970-01-01
      • 2018-02-12
      • 2019-12-29
      • 2016-07-06
      • 2018-07-21
      • 2018-08-04
      • 2012-12-11
      相关资源
      最近更新 更多