这是一个使用快速会话存储的简单连接,即使文本来自书中,它也会不断出现此错误。我很确定这与“new MongoStore”对象初始化有关。
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);