如何在Express中保存持久数据?

时间:2019-10-23 18:08:15

标签: express session cookies storage

我有一个简单的要求,将访问令牌/刷新令牌存储在Express中(不使用All is well until... ~/poky/build$ bitbake core-image-sato which results in this error: File "/usr/local/lib/python3.5/sqlite3/dbapi2.py", line 27, in <module> from _sqlite3 import * ImportError: No module named '_sqlite3' Below is my effort to proceed past this error, which didn't resolve the error above. Please be generous and provide some guidance. I searched for relevant posting locations; any advice on a better place is appreciated. Thank you. ------------------------------------------------ A web search on this error () results in: How to Use SQLite in Ubuntu | Chron.com with ~/poky/build$ sudo apt-get install sqlite3 libsqlite3-dev which tells me this: Reading package lists... Done Building dependency tree Reading state information... Done libsqlite3-dev is already the newest version (3.22.0-1ubuntu0.1). sqlite3 is already the newest version (3.22.0-1ubuntu0.1). The following packages were automatically installed and are no longer required: linux-headers-5.0.0-23 linux-headers-5.0.0-23-generic linux-image-5.0.0-23-generic linux-modules-5.0.0-23-generic linux-modules-extra-5.0.0-23-generic Use 'sudo apt autoremove' to remove them. 0 upgraded, 0 newly installed, 0 to remove and 12 not upgraded. So, evidently sqlite3 exists on my system. Here are the SO references that I checked: [ImportError: No module named '_sqlite3' in python3.3][1] [importerror no module named '_sqlite3' python3.4][2] [ImportError: No module named _sqlite3 (even after doing eveything)][3] [ImportError: No module named _sqlite3][4] [1]: https://stackoverflow.com/questions/20126475/importerror-no-module-named-sqlite3-in-python3-3 [2]: https://stackoverflow.com/questions/24052137/importerror-no-module-named-sqlite3-python3-4 [3]: https://stackoverflow.com/questions/35889383/importerror-no-module-named-sqlite3-even-after-doing-eveything [4]: https://stackoverflow.com/questions/2665337/importerror-no-module-named-sqlite3 或其他任何东西)。我想将它们存储在持久性localStorage Cookie中,这样,每次用户访问以前访问过该页面的页面时,都可以查看访问令牌是否已经存在,如果有,则进行API调用并登录等等。

我花了一些时间来研究httpOnlyexpress-session,但根本想不出正确的方法。 cookie-session需要一个用于生产的商店,我不想建立一个商店来仅存储访问令牌。因此,这样的事情在开发中有效:

express-session

使用它来根据要求进行设置:

app.use(
  session({
    secret: 'conduit',
    cookie: {
      path: '/',
      maxAge: 60 * 60 * 1000,
      httpOnly: true,
      secure: isProduction,
    },
    resave: false,
    saveUninitialized: false,
  })
)

但是,如果它不能在生产环境中工作,那就没有帮助。我无法使其与request.session.accessToken = accessToken request.session.save() 一起使用,或者我不知道如何设置/获取Cookie,并且文档也没有很大帮助。

所以我问:如何在不使用Store / Redis / MemCache / etc的情况下以持久的方式在Express服务器/ httpOnly cookie上存储一些字符串?

1 个答案:

答案 0 :(得分:0)

我必须使用cookieParser。

const cookieParser = require('cookie-parser')

app.use(cookieParser())

// set a cookie
response.cookie('nameOfCookie', 'cookieValue', {
  maxAge: 60 * 60 * 1000, // 1 hour
  httpOnly: true,
  secure: isProduction,
})

// get a cookie
request.cookies.nameOfCookie

// destroy cookie
response.clearCookie('nameOfCookie')