简体   繁体   English

Node Js 停止时如何查看错误?

[英]How can I see the error when Node Js has stopped?

I have a NodeJs server with nginx.我有一个带有 nginx 的 NodeJs 服务器。 It runs on ubuntu server.它在 ubuntu 服务器上运行。 it shows "502" from time to time.它不时显示“502”。 I am running it via pm2.我正在通过 pm2 运行它。 The real issue is now: when I log in and look at pm2 it doesn't show "error".现在真正的问题是:当我登录并查看 pm2 时,它没有显示“错误”。 When I run "restart", "502" goes away and starts working normally.当我运行“重启”时,“502”消失并开始正常工作。 Question:问题:
1.) Can I send an e-mail automatically when the server stops? 1.) 我可以在服务器停止时自动发送电子邮件吗?
2.) can I write every event to a file in the application? 2.) 我可以将每个事件写入应用程序中的文件吗? (Can I see the error by looking at the last event?) I am using MongoDB. (我可以通过查看最后一个事件来查看错误吗?)我正在使用 MongoDB。
3.) How can I catch the error? 3.) 我怎样才能发现错误?

There are multiple ways to do it.有多种方法可以做到。 You are not getting properly log and stopped/restarted the application that means your application needs code quality improvement and error handling properly.您没有正确记录和停止/重新启动应用程序,这意味着您的应用程序需要改进代码质量和正确处理错误。

You may use uncaughtException and unhandledRejection events to send email and log into a file您可以使用uncaughtExceptionunhandledRejection事件来发送电子邮件并登录到文件

process
  .on('unhandledRejection', (reason, p) => {
    //'Unhandled Rejection at Promise'
    // send email or write into file
  })
  .on('uncaughtException', err => {
    //'Uncaught Exception thrown'
    // send email or write into file
    process.exit(1);
  });

Apart from the above, you may use a third-party monitoring tool even pm2 also provides除了上面的,你可以使用第三方的监控工具,甚至pm2也提供

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM