node.js, catching errors so script doesn't break?

后端 未结 2 675
逝去的感伤
逝去的感伤 2021-01-12 04:31

I have a node.js script and anytime an error happens node.js stops running with the error that happened.

What is the proper way of checking errors in node.js so it w

2条回答
  •  死守一世寂寞
    2021-01-12 05:16

    You can catch otherwise uncaught errors by setting the following:

    process.on('uncaughtException', function (exception) {
       // handle or ignore error
      });
    

    Of course, if you run your script in something like forever or node-supervisor, when your script "stops" it will start back up, which might solve your problem.

提交回复
热议问题