我在Express中有一个错误处理middlware,试图捕获所有传入的错误:
app.use(function(err, req, res, next){ console.error(err.stack); res.status(500); res.render('500.jade'); });
但由于某些原因,每当我关闭mongod
进程时,我的应用程序崩溃时会出现以下堆栈跟踪:
Error: failed to connect to [localhost:27017] at null.(/ /node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:540:74) at EventEmitter.emit (events.js:106:17) at null. (/ /node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:140:15) at EventEmitter.emit (events.js:98:17) at Socket. (/ /node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:478:10) at Socket.EventEmitter.emit (events.js:95:17) at net.js:441:14 at process._tickCallback (node.js:415:13) Process finished with exit code 8
我尝试使用以下配置,但它没有帮助我:
var options = { server:{ auto_reconnect: true, poolSize: 10, socketOptions:{ keepAlive: 1 } }, db: { numberOfRetries: 10, retryMiliSeconds: 1000 } } mongoose.connect(config.db, options);
有人可能会想知道"如果没有数据库连接它的基本上不再运行,你为什么要让你的应用程序运行?".我不希望它崩溃并重新启动.在一定次数的尝试之后,Supervisor和Forever模块似乎停止尝试重新连接,从而使您的应用程序处于崩溃状态.
理想情况下,当MongoDB崩溃时,我想向用户显示500.jade错误页面,同时服务器应该每隔10秒继续尝试重新连接到数据库.重新连接后,恢复所有正常操作.
编辑:以下发布的解决方案都没有为我工作,域名除外.
这就是我处理Mongo失败的方法 - 将其添加为中间件.它还会尝试重新连接.
// Handler in case Mongo goes down app.use(function(req, res, next) { // We lost connection! if (1 !== mongoose.connection.readyState) { // Reconnect if we can mongoose.connect(config.db, options); res.status(503); throw new Error('Mongo not available'); } next(); });
这假设您在某处有一个标准的50x错误处理程序,它将向用户显示一个不错的页面.
重新连接用于下一个用户/页面加载,以检查它是否已备份.工作得很好.