根据我的经验,php服务器会向日志(log)或服务器端抛出异常,但不会向 node 抛出异常.js只是简单的崩溃.用try-catch来包围我的代码也不起作用,因为一切都是异步完成的.我想知道其他人在他们的生产服务器中做什么.

推荐答案

PM2

首先,我强烈建议为Node.js安装PM2.PM2在处理崩溃、监控 node 应用以及负载平衡方面非常出色.每当 node 应用程序崩溃、出于任何原因停止,甚至服务器重新启动时,PM2都会立即启动该应用程序.所以,如果有一天,即使在管理我们的代码之后,应用程序崩溃,PM2也可以立即重启它.欲了解更多信息,请点击Installing and Running PM2

其他答案真的很疯狂,因为你可以在http://nodejs.org/docs/latest/api/process.html#process_event_uncaughtexception点阅读Node自己的文档

如果有人正在使用其他说明的答案,请阅读 node 文档:

请注意,uncaughtException是一种非常粗糙的异常处理机制,将来可能会被删除

现在回到我们防止应用程序本身崩溃的解决方案.

因此,经过仔细研究,我最终得出了Node文档本身的建议:

不要用uncaughtException,而是用domainscluster.如果使用uncaughtException,请在每次未处理的异常后重新启动应用程序!

DOMAINCluster

我们实际上要做的是向触发错误的请求发送一个错误响应,同时让其他人在正常时间内完成,并停止侦听该工作进程中的新请求.

通过这种方式,域的使用与集群模块密切相关,因为当工作进程遇到错误时,主进程可以派生新的工作进程.看下面的代码来理解我的意思

通过使用Domain,以及使用Cluster将程序划分为多个工作进程的弹性,我们可以做出更恰当的react ,并以更大的安全性处理错误.

var cluster = require('cluster');
var PORT = +process.env.PORT || 1337;

if(cluster.isMaster) 
{
   cluster.fork();
   cluster.fork();

   cluster.on('disconnect', function(worker) 
   {
       console.error('disconnect!');
       cluster.fork();
   });
} 
else 
{
    var domain = require('domain');
    var server = require('http').createServer(function(req, res) 
    {
        var d = domain.create();
        d.on('error', function(er) 
        {
            //something unexpected occurred
            console.error('error', er.stack);
            try 
            {
               //make sure we close down within 30 seconds
               var killtimer = setTimeout(function() 
               {
                   process.exit(1);
               }, 30000);
               // But don't keep the process open just for that!
               killtimer.unref();
               //stop taking new requests.
               server.close();
               //Let the master know we're dead.  This will trigger a
               //'disconnect' in the cluster master, and then it will fork
               //a new worker.
               cluster.worker.disconnect();

               //send an error to the request that triggered the problem
               res.statusCode = 500;
               res.setHeader('content-type', 'text/plain');
               res.end('Oops, there was a problem!\n');
           } 
           catch (er2) 
           {
              //oh well, not much we can do at this point.
              console.error('Error sending 500!', er2.stack);
           }
       });
    //Because req and res were created before this domain existed,
    //we need to explicitly add them.
    d.add(req);
    d.add(res);
    //Now run the handler function in the domain.
    d.run(function() 
    {
        //You'd put your fancy application logic here.
        handleRequest(req, res);
    });
  });
  server.listen(PORT);
} 

虽然Domain是待定的弃用,并将被删除,因为新的替代品来了,如 node 的文档中所述

此模块正在等待弃用.一旦完成了替换API,该模块将被完全弃用.绝对必须拥有域提供的功能的用户可能暂时依赖它,但应该期望将来必须迁移到不同的解决方案.

但是,在没有引入新的替代方案之前,具有集群的域是 node 文档所建议的唯一好的解决方案.

要深入理解DomainCluster,请阅读

https://nodejs.org/api/domain.html#domain_domain (Stability: 0 - Deprecated)

https://nodejs.org/api/cluster.html

感谢@Stanley Luo与我们分享关于集群和域的精彩深入解释

Cluster & Domains

Node.js相关问答推荐

Node.js promise 循环中的所有多个API调用

无法在我的 node 项目中转让Google Drive v3 API中的所有权

请求正文未定义

对于具有重叠列的组合键,在冲突&q;上没有唯一或排除约束匹配错误

在对象的嵌套数组中使用$lookup,创建多个记录作为响应,mongodb

Socket.io 未将用户加入给定房间

将文件传递到 AWS lambdas(nodejs) + API 网关后重新上传文件

如何使用 $PATH 变量在系统中全局自动安装 bash 脚本?或者重写脚本到node

在 linux mint 上部署 node 应用程序的最简单方法是什么?

Mocha调用所有it回调模拟(测试中间件)

Mongodb聚合传递一个匹配的数组和一个不匹配的数组

如何更改NodeJS中的堆栈大小限制?

如何使用 mocha.js 模拟用于单元测试的依赖类?

异步函数 - 等待不等待promise

我应该在(Docker)容器中使用 forever/pm2 吗?

node/express:使用 Forever 连续运行脚本时设置 NODE_ENV

TypeError:winston.Logger 不是带有winston 和morgan 的构造函数

Node.js 中空函数的简写

mongo 可以 upsert 数组数据吗?

如何从 find 方法返回 Mongoose 结果?