【问题标题】:Node.js server crashing without error messageNode.js 服务器崩溃而没有错误消息
【发布时间】:2013-11-09 19:55:14
【问题描述】:

我有一个 Node.js 服务器,它不断崩溃而没有记录任何类型的错误消息。这是典型的场景吗?如何在错误崩溃之前捕获错误并记录它?

【问题讨论】:

  • 除了记录未捕获的异常以查找崩溃原因外,您还可以检查进程是否内存不足。
  • 您能推荐一种检查服务器内存使用情况的方法吗?
  • 两年前我这样做的方式是从 shell 运行我的节点服务器并捕获 stderr。当时的 V8 会毫不客气地退出并向 stderr 写入错误消息。但是,V8 已经发生了很大的变化,我不知道这种技术是否会继续起作用。如果长时间后发生崩溃,监控进程内存可能会有所帮助。

标签: node.js


【解决方案1】:

一个好的开始是设置,尤其是在生产环境中,在为您的服务器设置侦听器之前设置一个记录详细信息的异常处理程序。看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 ?
});

如果您使用的是 Express.js,请查看 here 以了解如何查看您的错误的完整堆栈(如果您在生产中,最终再次将其发送到您的电子邮件)。 在这种情况下,告诉它在实例化监听器之前给你完整的细节:

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 年更新:您需要安装 errorHandler package

【讨论】:

  • errorHandler 中间件不再与 express 捆绑在一起。你可以通过 rinning npm install errorhandler 来安装它——在这里查看详细信息github.com/expressjs/errorhandler
  • 原来是SQL默默地让进程崩溃了,我不知道。因此,多亏了这一点,我才得以追踪到这一点。谢谢
【解决方案2】:

如果有人遇到与我类似的问题:我有一个崩溃的 Node.js 服务器,根本没有任何错误,并且在意识到这是因为在我的代码中我正在写信的某个地方之前拉了一个小时的头发像writeFileSync("./foo.json", "…") 这样的文件,这当然可以,但这会导致服务器“崩溃”,因为我使用 PM2 来“监视”服务器目录的文件更改 - 即每当服务器目录中的文件更改时, PM2 重启服务器。我通过为.data 文件夹添加watch_ignore 配置并将foo.json 放在那里解决了这个问题。

【讨论】:

    【解决方案3】:

    要完成@matteofigus 的回答,你也可以listen for unhandled promise rejections

    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`
    

    【讨论】:

      【解决方案4】:

      节点 v6.11.0,Windows 10。

      在这里尝试了其他建议无济于事 - 应用程序只是停止,即使使用也没有错误

      process.on('uncaughtException',...)
      process.on('unhandledRejection',....)
      

      最终跟踪退出/崩溃到递归函数调用。下面的代码演示了这个问题;

      "use strict" ;
      
      process.on('uncaughtException', function (exception) {
        console.log(exception); 
      });
      
      var count = 0 ;
      function recursiveFunction(){
          console.log(count++);
          recursiveFunction();
      }
      recursiveFunction() ;
      

      这将运行到目前为止然后停止。 Try/Catch 也不起作用 - 如上所述使用 ;

      function recursiveFunction(){
          console.log(count++);
          try{
              recursiveFunction();
          }
          catch(e){
              console.log("recursion error");
          }
      }
      

      什么都没有 - 只是停下来。

      一种解决方法(无需重新设计代码)是使用 setImmediate(避免递归过程);

      function recursiveFunction(){
          console.log(count++);
          setImmediate(recursiveFunction);
      }
      

      (我最终通过 ctrl-c 来阻止它。)

      报告于node github issues

      【讨论】:

        【解决方案5】:

        您可以使用名为“errorhandler”的中间件和“logger”(log4js-npm-package),您可以记录所有错误异常。这是错误处理程序的代码:

        // 中间件:包罗万象的错误处理程序。这样我们就可以记录错误,但不会将内部错误详细信息泄露给客户端。

        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;
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多