Cannot read property 'Store' of undefined nodejs express session
Express在使用mongodb的时候app配置出错! "Cannot read property ‘Store‘ of undefined"
原因主要是express版本4++问题
//settings.js
module.exports={
cookieSecret:"xxxx",
db:"dbname",
host:"localhost",
}
//app.js
var express = require("express");
var settings = require("./settings");
加载connect-mongo的出错写法:
var MongoStore=require("connect-mongo")(express);
app.use(express.session({
secret:Settings.cookieSecret,
key:Settings.db,
cookie:{maxAge:1000*60*60*24*30},//30 days
store:new MongoStore({
db:Settings.db
})
}));
启动后报错:TypeError: Cannot read property ‘Store‘ of undefined
以上是 express<4的写法
express 4.0之后(包括4.0)写法如下:
var session = require("express-session");
var MongoStore=require("connect-mongo")(session);
app.use(session({
secret:settings.cookieSecret,
store:new MongoStore({
db:settings.db
})
});
package.json配置如下
{
"name": "dbTest",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"express": "~4.2.0",
"static-favicon": "~1.0.0",
"morgan": "~1.0.0",
"cookie-parser": "~1.0.1",
"body-parser": "~1.0.0",
"debug": "~0.7.4",
"ejs": "~0.8.5",
"mongodb":"*",
"connect-mongo":"*",
"express-session":"*"
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。