2015-04-01 52 views
0

我有使用nodejs实现的服务器。根据这个post,我在我的index.js文件中有一个连接(使用本地MongoDB),它在服务器启动时连接到MongoDB。在粉碎后重新启动MongoDB Connecton - NodeJS

我一直在使用,所以每次服务器压碎这个模块重新启动它。

我的问题是:如果我的mongo连接会粉碎,会发生什么?我该如何处理这种情况?我怎样才能以编程方式检测粉碎?我应该重新启动它吗,如果是的话,怎么样?

+0

你使用的是mongoose.js吗? – 2015-04-01 19:04:16

+0

@Kiran Pagar不,我使用本地mongodb(MongoClient = require('mongodb')。MongoClient)。 http://mongodb.github.io/node-mongodb-native/api-generated/mongoclient.html – 2015-04-01 19:08:15

+0

对于猫鼬,你可以使用这样的东西 - http://stackoverflow.com/questions/29342904/error-no- open-connections-at-db-executequerycommand-node-js/29347440#29347440 - 必须有一些类似的方法可以与本机一起使用。 – 2015-04-01 19:10:31

回答

0

您可以将自动连接参数设置为true。

app.use(session({ 
store: new MongoStore({ 
    // Basic usage 
    host: 'localhost', // Default, optional 
    port: 27017, // Default, optional 
    db: 'test-app', // Required 

    // Basic authentication (optional) 
    username: 'user12345', 
    password: 'foobar', 

    // Advanced options (optional) 
    autoReconnect: true, // Default 

}) 

}));

+0

谢谢,我会用这个。 – 2015-04-03 06:48:21