Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to restart a node.js server after a change is made to its application script

Tags:

node.js

So, I'm in the process of learning node, and am finding that for testing, I have to kill the running instance of node, then start it up again with the new application code for the server. So, I first execute my node server like this: node myapp.js.

Then, I notice something isn't working right, or I want to add some new code to myapp.js. I go into Process Explorer (I'm running node on a Windows box), and kill the node.exe process, and then upload the new myapp.js file with the changes. Then I do node myapp.js to start it again to test again.

There has got to be an easier way to do this. I typed in node --help but there are too many flags listed to see if there is some kind of restart mechanism similar to apache on linux like: httpd -k restart. Is there a kind of "graceful" restart with node?

like image 859
ariestav Avatar asked Mar 06 '12 02:03

ariestav


People also ask

Which command will you run to restart your node server after making some changes?

You can restart the process at any time by typing rs and hitting ENTER . Once you make the changes to package. json , you can then call nodemon to start the example app in watch mode without having to pass in server.

How do I restart a node js server?

If it's just running (not a daemon) then just use Ctrl-C . Where PID is replaced by the number in the output of ps . You could also use "killall -2 node", which has the same effect.

How do I restart node server automatically?

Running non-Node code While Nodemon is running, we can manually restart our application. So instead of stopping and restarting Nodemon, we can just type rs and press enter, and Nodemon will restart the server or the running process for us.

How do I restart a react js server?

In this case, if we make any changes to the project then we will have to restart the server by killing it using CTRL+C and then typing the same command again.


2 Answers

Check out nodemon. Install it using npm install nodemon, after that simply use nodemon server.js if server.js is the file you're working on.

like image 145
lowerkey Avatar answered Sep 20 '22 18:09

lowerkey


Use node-supervisor

like image 32
Bartek Avatar answered Sep 21 '22 18:09

Bartek