1
index.js:無法使用連接 - 蒙戈會話存儲現有的本地蒙戈-DB連接
..
var MongoStore = require('connect-mongo')(session);
var db = require(path.join(__dirname, 'db.js'));
..
db.connect(conStr, function(err) {
if(err) {
console.log('Connect to mongoDb failed!');
process.exit(1);
} else {
app.listen(port, function() {
console.log('App listening on port: ' + port);
});
}
});
...
app.use(session ({
name: 'bbn.sid',
secret:'shopusersessionsecret',
resave: false,
saveUninitialized: false,
store: new MongoStore({ db: db.get() }),
cookie: {
maxAge: 180 * 60 * 1000,
}
}));
..
db.js:
...
var database = null;
function connect(url, done) {
if(database) return done();
MongoClient.connect(url, function(err, db) {
if(err) return done(err);
database = db;
done();
});
}
function get() {
return database;
}
...
如果我創建到會話存儲的新連接(通過提供url) 它似乎確定,但如果我試圖使用現有的mongo連接,我得到以下錯誤:
E:\Proj\bbn\node_modules\connect-mongo\src\index.js:105
throw new Error('Connection strategy not found');
^
Error: Connection strategy not found
at MongoStore (E:\Proj\bbn\node_modules\connect-mongo\src\index.js:105:23)
at Object.<anonymous> (E:\Proj\bbn\index.js:50:12)
at Module._compile (module.js:541:32)
看來,會話中間件調用,而不是然後mongo db連接初始化?