Node Forever JS on Any Virtual Machine

Host your web apps forever from any Virtual Machine

Narendiran Krishnan
Geek Culture
5 min readMar 15, 2021

--

credit goes to some random dude in this world

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.

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,

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

Server js code

Once you do this just verify whether it is working fine or not

verify whether it works without error

Now visit to the browser with the IP which is assigned to your EC2 instance so that you can verify that as shown below

verify from your browser

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 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,

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.

forever stop cmd

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 0

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 stopall

Forever Logs

If you wish to find the logs, then you can just use the following code

It will list the list, the path of each and every script which is running along with the uid.

forever logs

Now that we stop the forever script and verify our browser and it should throw up the following screen,

killall node

For this you can use any commands, i.e. the normal node command or forever command,

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:

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..!!!

--

--

Narendiran Krishnan
Geek Culture

AI blogger. Inspiring & working towards a better future through technology & Artificial Intelligence. Join me in the quest ..!!