Node.js 服务器崩溃但没有错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19882344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Node.js server crashing without error message
提问by Rick Eyre
I have a Node.js server that is continually crashing without logging any kind of error message. Is this a typical scenario? How can I trap the error and log it before it crashes?
我有一个 Node.js 服务器,它在不记录任何错误消息的情况下不断崩溃。这是一个典型的场景吗?如何在错误崩溃之前捕获错误并记录它?
回答by matteofigus
A good start would be to setup, especially in production, before setting the listener for your server, an handler for the exceptions that logs the details. Look at here:
一个好的开始是设置,尤其是在生产中,在为您的服务器设置侦听器之前,记录详细信息的异常处理程序。看看这里:
process.on('uncaughtException', function (exception) {
console.log(exception); // to see your exception details in the console
// if you are on production, maybe you can send the exception details to your
// email as well ?
});
If you are using Express.js, take a look at hereto know how to see the full stack of your error (and eventually, again, send it to your email if you are on production). In that case, tell it to give you the full details before instantiating the listener:
如果您使用 Express.js,请查看此处以了解如何查看您的错误的完整堆栈(如果您正在生产,最终再次将其发送到您的电子邮件)。在这种情况下,请告诉它在实例化侦听器之前为您提供完整的详细信息:
var express = require('express');
// ...
var app = express();
var errorhandler = require('errorhandler')
// ...
app.use(errorHandler({ dumpExceptions: true, showStack: true }));
// then, set the listener and do your stuff...
2019 update: you'll need to install the errorHandler package
2019 更新:您需要安装errorHandler 包
回答by Alexis N-o
To complete @matteofigus answer, you can also listen for unhandled promise rejections.
要完成@matteofigus 的回答,您还可以侦听未处理的承诺拒绝。
process.on('unhandledRejection', (reason, p) => {
console.log("Unhandled Rejection at: Promise ", p, " reason: ", reason);
// application specific logging, throwing an error, or other logic here
});
somePromise.then((res) => {
return reportToUser(JSON.pasre(res)); // note the typo (`pasre`)
}); // no `.catch` or `.then`
回答by Bob
Node v6.11.0 , Windows 10.
节点 v6.11.0,Windows 10。
Tried the other suggestions here to no avail - app just stops, no error even using
在这里尝试了其他建议无济于事 - 应用程序只是停止,即使使用也没有错误
process.on('uncaughtException',...)
process.on('unhandledRejection',....)
Finally tracked exit/crash down to a recursive function call. The following code demonstrates the problem ;
最后跟踪退出/崩溃到递归函数调用。下面的代码演示了这个问题;
"use strict" ;
process.on('uncaughtException', function (exception) {
console.log(exception);
});
var count = 0 ;
function recursiveFunction(){
console.log(count++);
recursiveFunction();
}
recursiveFunction() ;
This will run so far then just stop. Try/Catch didn't work either - tried as above with ;
这将运行到目前为止然后停止。Try/Catch 也不起作用 - 像上面一样尝试使用 ;
function recursiveFunction(){
console.log(count++);
try{
recursiveFunction();
}
catch(e){
console.log("recursion error");
}
}
Again nothing - just stops.
再次没有- 只是停止。
A workaround (without having to redesign the code) is to use setImmediate (to avoid the recursion process);
一种解决方法(无需重新设计代码)是使用 setImmediate(避免递归过程);
function recursiveFunction(){
console.log(count++);
setImmediate(recursiveFunction);
}
(I eventually ctrl-c'd this to stop it.)
(我最终 ctrl-c'd 这个来阻止它。)
reported at node github issues
回答by Krishna Ghatul
You can use middleware called 'errorhandler' & with 'logger' (log4js-npm-package) you can keep log for all error exceptions. Here's the code for Errorhandler :
您可以使用名为“errorhandler”和“logger”(log4js-npm-package)的中间件,您可以保留所有错误异常的日志。这是 Errorhandler 的代码:
// Middleware: Catch-All Error Handler. So that we log errors, but don't leak internal error details to the client.
// 中间件:Catch-All 错误处理程序。以便我们记录错误,但不会将内部错误详细信息泄露给客户端。
app.use(errorHandler);
function errorHandler(err, req, res, next) {
// XHR Request?
if (req.xhr) {
logger.error(err);
res.status(500).send({ error: 'Internal Error Occured.' });
return;
}
// Not a XHR Request.
logger.error(err);
res.status(500);
res.render('framework/error', { error: "Internal Server Error." });
// Note: No need to call next() as the buck stops here.
return;
}

