我express-session在我的 Node.js Web 應用程式上為用戶使用登錄系統。我的問題是,每次我進行更新并重新啟動 Node.js 服務器時,所有用戶都被注銷,這不是理想的行為(有多個用戶通過本地網路登錄,我希望不必登錄它們每次重新啟動時重新打開)。
根據我的理解,我可以使用諸如connect-pg-simple(作為參考,我在節點 Web 應用程式中使用 postgres 資料庫)來保持用戶在服務器重啟時登錄,但我不確定如何實作這一點。
我知道這將是類似的事情:
app.use(session({
store: new (require('connect-pg-simple')(session))({
// Insert connect-pg-simple options here
}),
secret: 'secret',
resave: false,
saveUninitialized: true
}));
但我不知道使用什么選項或如何在服務器重啟時保存用戶會話。
uj5u.com熱心網友回復:
是的,你走在正確的軌道上。
- 打開您的資料庫并創建一個名為 session 的表。請參閱:https : //github.com/voxpelli/node-connect-pg-simple/blob/HEAD/table.sql
- 添加像您發布的 connect-pg-simple 代碼
- 從 node-pg 傳遞您正在使用的 postgres 池。
const session = require('express-session') const PGSessionStore = require('connect-pg-simple')(session) const pg = require('pg')
const pool = new pg.Pool({
user: process.env.PG_USERNAME,
host: process.env.PG_HOST,
database: process.env.PG_DATABASE,
password: process.env.PG_PASSWORD,
port: process.env.PG_PORT
})
app.use(session({
store: new PGSessionStore({
pool: pool,
tableName: 'session'
}),
secret: process.env.COOKIE_SECRET,
cookie: {
secure: false,
httpOnly: true,
sameSite: true,
maxAge: 24 * 60 * 60 * 1000
},
saveUninitialized: true,
resave: false
}))
uj5u.com熱心網友回復:
您的會話存盤在應用程式運行時中,因此在重繪 時,它會在服務器重新啟動時重置它在運行時可能保留的所有其他資料
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/327510.html
標籤:节点.js 连接 快速会话 节点postgres
下一篇:當sendFile()只包含網站上一個特定html檔案的路徑時,為什么我的NodeJSExpress應用程式為整個網站提供服務
