简体   繁体   English

如何使用Forever和Nodemon(Windows)自动重启Node.js应用程序

[英]How can I automatically restart a Node.js application using Forever and Nodemon (Windows)

I am running a node.js application in Windows and I want to make it automatically restart if there is an unhandled exception in the code which causes the application to stop. 我正在Windows中运行node.js应用程序,并且如果代码中有未处理的异常导致该应用程序停止,我想使其自动重启。

I have done some research and I found that a combination "Forever" and "Nodemon" can achieve this goal. 我进行了一些研究,发现“ Forever”和“ Nodemon”的组合可以实现此目标。

I installed both packages globally on my Windows 10 Device. 我在Windows 10设备上全局安装了这两个软件包。

npm install forever -g
npm install -g nodemon

I tried using the following command to launch my app: 我尝试使用以下命令启动我的应用程序:

forever start nodemon --exitcrash app.js

However, I get the following error: "nodemon does not exist" 但是,出现以下错误:“ nodemon不存在”

If try just running "nodemon" the application starts which indicates the Nodemon package is installed however, this will not allow the app to restart after a crash. 如果尝试仅运行“ nodemon”,则应用程序启动,这表明已安装Nodemon软件包,这将导致崩溃后该应用程序无法重新启动。

Am I doing something wrong? 难道我做错了什么? Most advice I find online is only relevant to Linux systems. 我在网上找到的大多数建议仅与Linux系统有关。

If you are already using forever, then you can get rid of nodemon. 如果您已经永久使用过,则可以摆脱nodemon。 Instead you can use a combination of forever and cluster module. 相反,您可以结合使用永久模块和集群模块。 Simply fork the worker in case of exceptions, and it makes your app more scalable too! 只需在异常情况下派遣工作人员,它就可以使您的应用程序更具可扩展性!

If still nodemon is preferable, maybe try installing it globally using the -g flag 如果仍然最好使用nodemon,则可以尝试使用-g标志在全局进行安装

Forever and nodemon achieve 2 completely different objectives 永远和nodemon实现两个完全不同的目标

  • nodemon is used to run your application in development mode, where you are frequently changing code, and need to restart the server .It will not restart your application in case of a crash. nodemon用于在开发模式下运行您的应用程序,在该模式下您经常更改代码,并且需要重新启动服务器。如果发生崩溃,它将不会重新启动您的应用程序。 more about that later 以后再说

  • Forever, on the other hand, is for making your application run as a daemon in production. 另一方面,永远是使应用程序在生产环境中作为守护程序运行。 And auto restart if you have uncaught exceptions. 如果有未捕获的异常,并自动重启。

  • Historically people have used Forever stand alone, or with upstart scripts, running as a linux service one of the most famous being upstart 从历史上看,人们一直使用Forever独立版本或与暴发户脚本一起使用,作为Linux服务运行,其中最著名的就是暴发户

  • Current norm is to use PM2 当前规范是使用PM2

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

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