這裏是使用快速會話存儲的簡單連接,即使書中的文本是正確的,它仍會使此錯誤不再出現。我很確定這與'新的MongoStore'對象初始化有關。錯誤:未找到連接策略MongoDB
var express = require('express'),
expressSession = require('express-session');
var MongoStore = require('connect-mongo/es5')(expressSession);
var sessionStore = new MongoStore({
host: '127.0.0.1',
port: '27017',
db: 'session'
});
var app = express()
.use(expressSession({
secret: 'my secret sign key',
store: sessionStore
}))
.use('/home', function (req, res) {
if (req.session.views) {
req.session.views++;
}
else {
req.session.views = 1;
}
res.end('Total views for you:' + req.session.views);
})
.use('/reset', function(req, res) {
delete req.session.views;
res.end('Cleared all your views');
})
.listen(3000);
看起來像'db:'session''這樣的行沒有任何影響。我評論了這一行,並且剛剛添加了@alturium所說的「url」選項,它仍然工作。實際上它會在'demo'數據庫中創建一個名爲'sessions'的集合。 –