Node Forever JS on Any Virtual Machine
Host your web apps forever from any Virtual Machine
If you ever wanted to host your Node.js application forever, then in this right place. Here you will learn how to run your Node js app forever, even when the console is closed.
Forever → A simple CLI tool for ensuring that a given script runs continuously (i.e. forever)
If you have ever faced an issues as shown below while using Virtual Machine, then, this blog will help you to overcome the problem.
“ client_loop: send disconnect: Broken pipe “
Yes, this happens always while working with cloud and this is where the package Forever Js comes in handy.
Here I will uncover all the useful commands which are required for Forever Js. Now let’s get started, but before getting started, let install forever js.
Code is as follows,
sudo apt-get updatesudo sunpm i forever -g
Next step is to create a test node js application which we will use for this forever application.
Note:
Guys here I am using an EC2 Server instance and as usual, I just grabbed whatever is under the free tier limit i.e. Ubuntu or any Linux based OS.
Here is the code, first create a server.js file
Once you do this just verify whether it is working fine or not
Now visit to the browser with the IP which is assigned to your EC2 instance so that you can verify that as shown below
Alright, I guess we now have our beloved server running successfully.
Now all that is left is just to make use of the forever js lib.
Forever Commands
Forever Start
Usually we start our server by running the code as node server.js but here there will be a small change using the code while running and the code is as follows,
forever start server.js
Forever list
Alright, now we can verify using the forever list command
forever list
This command is actually used for listing all the scripts which are running forever and it doesn’t stop here. It also shows us location, process id, up-time and also the details of the log file name of each script that’s running.
Forever Stop
The next command is the stop, yes, if there is a start, then there will be a stop definitely.
We can stop using the file name of the script you want to stop.
The code is as follows,
forever stop server.js
With this command you have to reach the specific location of the file where the node application is running and only then stop or else it will not work properly.
Alright, what if you want to stop a file based on uid.
Forever stop uid
Well, whenever you start or else list using the forever command you will be displayed with an output of whichever script is running with a uid. So we can also stop using those commands rather than typing out the name of the script file. You can verify by using the start or list command. Now type the following commands,
forever stop o
Even here you can see the uid which I spoke about.
Forever Stopall
Alright, if you wish to stop everything that’s running simple stopall, but there are two commands one is forever command and the other one is a usual node command.
Forever Logs
If you wish to find the logs, then you can just use the following code
forever logs
It will list the list, the path of each and every script which is running along with the uid.
Now that we stop the forever script and verify our browser and it should throw up the following screen,
For this you can use any commands, i.e. the normal node command or forever command,
killall nodeforever stopall
Now you can use the forever start command and close the EC2 instance terminal and then verify for yourself.
I guess I have given details of most of the Forever js Commands and the rest of the few other commands I will give you guys and do try it out.
Commands:
forever restart server.jsforever restart 0 --> restarts uid o script aloneforever restartall --> restarts all running forever scriptsforever -h --> -h, –help: Command line help informationforever config --> lists all forever user configurationsforever clear --> clears the specified forever config by its keyforever cleanlogs --> deletes all historical forever log filesforever set --> defines a forever config value for an unique key
You can find the server.js file in my GitHub.
GitHub Link
If you want to learn how certificate binding is done for web apps using openssl then follow the blog below,
If you wish to stay connected,
you can just google “ narenltk / narendiran krishnan ” or just drop a mail to → narenltk@gmail.com → Happy to help..!!!