the*_*lqd 1 error-handling node.js uncaught-exception express pm2
我正在使用 pm2 来管理我的 nodejs express 应用程序(以集群模式运行)中的进程。
我们有两种错误处理程序
第一:'uncaughtException' 将被处理
process.on('uncaughtException', function(err){});
Run Code Online (Sandbox Code Playgroud)实际上,我没有声明这样的处理程序,因为在这种情况下让 pm2 检测到死亡的工人,因此会自动重新启动死亡的工人。
第二:express error handler,我的意思是错误将被转发到express error handler,而不是uncaughtException handler,错误处理程序如下
app.use(function(err, req, res, next) {})
Run Code Online (Sandbox Code Playgroud)我也没有为了与 uncaughtException 相同的目的声明这个错误处理程序。但是在这种情况下 pm2 不会重新启动节点。
对这个问题有什么想法吗?非常感谢
当使用 express 错误处理程序甚至“uncaughtException”事件捕获错误时,进程仍在运行,因此 pm2 不会重新启动它。如果您希望 pm2 在每次异常后重新启动,我会建议如下:
process.on('uncaughtException', function(e) {
console.log('An error has occured. error is: %s and stack trace is: %s', e, e.stack);
console.log("Process will restart now.");
process.exit(1);
})
Run Code Online (Sandbox Code Playgroud)
快速错误处理程序也是如此。当我们执行 process.exit 方法时,进程将终止并且 pm2 将重新启动它。