If you have followed my YouTube channel lately, you probably know that I am heavily studying on Web Development. I do this out of two reasons, first, I want to better understand how Web Applications work so I can be a better Penetration Tester and second, personal interest. I always wanted to learn how to code and I really enjoy building Web Apps because I am more of a visual learner. Anyway, as soon as I started with Node.js, I had to find a way to Automate Node Server Restart after doing any changes in the code.
Usually you would need to to run
to start your server and once you did some changes on the code, you would need to CTRL+C out of it and run the above command again to restart it.
Gladly, there is a very easy way to automate this process using a tool called Nodemon.
Automate Node Server Restart
I went ahead and created a very simple Node app with some Demo Text.
Now, we could go ahead and start our Node.js server manually by running:
And we would be able to open our "Website" by navigating to localhost:3000 on a web browser. However, if we did some changes now on our code and want to check them in the browser, we would have to need to hit CTRL+C to kill the server and then manually restarting it again by running node app.js again. We are going to automate this process now.
Automate Node Server Restart with Nodemon
It is as simple as running one single command. First, we need to install Nodemon. Make sure you are in your project folder and run:
npm i -g nodemon
Once you have this done, from now on you can start your application with:
Nodemon is waiting for you to save any new edits to your code. I will go ahead and change the Demo Text in my code and hit save. See what happens.
As soon as I hit the save button, Nodemon recognizes it and automatically restarts the server.
The only thing you have to do now is to refresh your web browser and the changes will be displayed, without you having to manually restart your server.
As you can see, it is really easy to Automate Node Server Restart with the help of Nodemon. Moving forward, I will always use this tool from now on in my development workflow.