1. 方法
修改store中url的路径,即将url中‘settings.db’修改为:‘mongodb://localhost/’+settings.db。
报错具体内容:
\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
源码:
app.use(session({
store: new MongoStore({
//db: settings.db
url: 'settings.db'
})
}));
源码修改:将url中‘settings.db’修改为:‘mongodb://localhost/’+settings.db
app.use(session({
store: new MongoStore({
//db: settings.db
url: 'mongodb://localhost/blog'
})
}));
2. 原理:
Create a new connection from a MongoDB connection string
MongoDB connection strings are the best way to configure a new connection. For advanced usage, more options can be configured with mongoOptions
property.
// Basic usage
app.use(session({
store: new MongoStore({ url: 'mongodb://localhost/test-app' })
}));
// Advanced usage
app.use(session({
store: new MongoStore({
url: 'mongodb://user12345:foobar@localhost/test-app?authSource=admins&w=1',
mongoOptions: advancedOptions // See below for details
})
}));
参考connect-mongo文档:https://github.com/jdesboeufs/connect-mongo#create-a-new-connection-from-a-mongodb-connection-string