Express Sessions and MySQL



我现在正试图围绕会话进行思考。目前,我有一个 express/NodeJS 应用程序,它使用查询参数来存储脚本中的值,如下所示

脚本 片段:

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、node js 和 mysql,把我最好的一面放在这个项目上。任何帮助将不胜感激!

对于 mysql 的 Express-session,您可以使用像 https://www.npmjs.com/package/express-mysql-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
})

最新更新