Node. js development USES the node Supervisor implementation to monitor file modifications and automatically restart the application

  • 2020-03-30 04:14:38
  • OfStack

When developing or debugging node. js applications, when you modify the js file, you always have to press CTRL+C to terminate the program, and then restart it. Is there a way to save time by automatically restarting (or reloading) node. js after the file is modified? I started with the idea of using the grunt watch module to monitor file changes, but then I looked it up on the Internet and it turned out that what we had in mind had already been thought of and done well. (link: https://github.com/isaacs/node-supervisor) is just such a can realize the demand of the Node. Js module.

According to the instructions on Github, the Node Supervisor was originally intended to restart itself when the node.js application on the server crashes. Of course, it can also monitor your project's js (or CoffeeScript) file changes, and then restart to facilitate debugging the application.

Installation method (global module installation) :


npm install supervisor -g

Assuming your node.js main entry is app.js, simply execute the following command to start monitoring file changes.


supervisor app.js


//The folder or js file to monitor defaults to '.'
-w|--watch <watchItems> //The folder or js file you want to monitor is ignored. < br / > -i|--ignore <ignoreItems> //The time interval (cycle) for monitoring file changes, by default, is node. js built-in time
-p|--poll-interval <milliseconds> //The default file extension to monitor is 'node|js'
-e|--extensions <extensions> //The default for the main application to execute is 'node'
-x|--exec <executable> //Turn on debug mode (use --debug flag to start node)
--debug //Quiet mode, no DEBUG information
-q|--quiet

Example:


supervisor myapp.js
supervisor -w py_scripts -e 'py' -x python myapp.py
supervisor -w lib, server.js, config.js, server.js

Similar products and realize the same function (link: https://github.com/DTrejo/run.js) and (link: https://github.com/remy/nodemon), both I used. However, from the perspective of documentation, the former, like Supervisor, can be used in a minimum of 5 minutes, and its function is slightly weaker than that of Supervisor. The latter features more, the corresponding document is very long, it is estimated that it will take at least half an hour to study through. Which one to choose depends on the project requirements and personal preferences.


Related articles: