参考一篇博客学习express开发,但是个人用的是express 4.0,在使用connect-mongo时候出现如下错误: \node_modules\connect-mongo\src\index.js:100 throw new Error(‘Connection strategy not found’); ^
Error: Connection strategy not found at MongoStore (E:\chenjsh36\mydevelop\node\newexpress_6\node_modules\connect-mongo\src\index.js:100:23) at Object.<anonymous> (server.js:36:9) at Module._compile (module.js:435:26) at Object.Module._extensions…js (module.js:442:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:311:12) at Function.Module.runMain (module.js:467:10) at startup (node.js:134:18) at node.js:961:3
上网查了资料,并根据express的插件独立做了修改,但还是不行并报了以上的错误,mongo数据库已经安装也连接过: require connect = require(‘connect’) , cookieParser = require(‘cookie-parser’) , session = require(‘express-session’) , MongoStore = require(‘connect-mongo’)(session) , settings = require(’./module/setting’) ; app.use(cookieParser); app.use(session({ secret: settings.cookieSecret, store: new MongoStore({ url: settings.host }) }));
大牛求教是什么原因?
我也遇到一样的问题! 我用 store: new MongoStore({ //db:settings.db url:‘mongodb://localhost/’+settings.db, autoRemove:‘native’ }) 替代 store: new MongoStore({ url: settings.host }) 就可以了
app.use(session({
secret: settings.cookieSecret,
key: settings.db,//cookie name
cookie: {maxAge: 1000 * 60 * 60 * 24 * 30},//30 days
resave: false,
saveUninitialized: true,
store: new MongoStore({
/*db: settings.db,
host: settings.host,
port: settings.port*/
url: 'mongodb://localhost/blog'
})
}));
“connect-mongo”: “~1.0.2”,另外我也在跟着这个走https://github.com/nswbmw/N-blog,走通的代码我都放到Github上了,有兴趣的可以看一下,互相帮助.
connect-mongo 有說明如何 new 新的 connection
new MongoStore 裡面包的應該要是個 url
所以應該改成:
new MongoStore: { url: 'your_db_url'}
參考: https://github.com/kcbanner/connect-mongo#create-a-new-connection-from-a-mongodb-connection-string