gpt4 book ai didi

javascript - Node.js 重启后保持 Session

转载 作者:太空宇宙 更新时间:2023-11-04 00:53:51 25 4
gpt4 key购买 nike

我正在尝试获取 session 以在 Express v4 中存储护照 session ,但在服务器重新启动后它不会持续存在。我已经在使用 connect-mongo 所以我不清楚为什么它目前没有持续存在。

db.js

module.exports = function(app, config) {
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
session = require('express-session'),
MongoStore = require('connect-mongo')(session),
fs = require('fs');

mongoose.connect(config.db);

mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
mongoose.connection.once('open', function callback() {
console.info('Database Connected!');
});

var sessionStore = new MongoStore({mongooseConnection: mongoose.connection});
module.exports = sessionStore;
};

express.js

module.exports = function (app, config, passport, dbConnect) {
/* #Flash messaging and sessions ------------------------------------------------- */
app.use(cookieParser('secretStrings'));
app.use(session({
secret: 'secretStrings',
resave: true,
saveUninitialized: true,
cookie: {
path: '/',
httpOnly: true,
secure: false,
maxAge: null
},
rolling: true,
store: dbConnect
}));
app.use(flash());

/* #Set up passport ------------------------------------------------- */
// use passport session
app.use(passport.initialize());
app.use(passport.session());
};

Server.js

/* #Mongo --------------------------------------------------------------------- */
var db = require('./app/db')(app, config);

/* #Express Settings --------------------------------------------------------------------- */
require('./config/express')(app, config, passport, db);

我认为对此唯一合乎逻辑的解释是我以错误的顺序包含了文件,因此无法保存 session ?

最佳答案

sessionStore 需要在 db.js返回,而不是 module.exports = sessionStore; .

关于javascript - Node.js 重启后保持 Session,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30963936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com