Welcome back to this tutorial on launching a Node API to the cloud with Digital Ocean. Yesterday we configured our FreeBSD servers so now it's time to get our hands dirty with PM2. We'll be launching our core API today then learning how to monitor and enhance our API's performance.
So let's crack open the terminal and get started.
ssh freebsd@your/node1/ip/address cd todo-api pm2 start app.js --name MyApi
Good, now let's see what just happened. First, we just used PM2 to start the app in much
the same way we would start it normally. Only now, the app is running as a daemon in the
background so we aren't holding up our shell. Not only that, but we can see that we now
have this image displayed by PM2 which shows our app name, process id, status, cpu usage,
uptime, and memory usage. We can view this at any time with the command
pm2 stop MyApi.
Now let's try something else.
pm2 stop MyApi
Now we have stopped our API from running and you can see that we our display list shows
the status as
stopped. Now let's restart the api.
pm2 start MyApi
That's it, now our api is running again. So how would get rid of an app process altogether?
pm2 delete MyApi
Now we have removed the process entirely. Knowing this, let's remake our app process using a neat feature of PM2.
pm2 start app.js --name MyApi -i 0
Take notice of the
-i 0. This means we have launched our app in cluster mode. What does
this mean? It means the app will be scalled across all available CPUs on your system. So
if you have 4 CPUs available, then you will have 4 instances of your app process running
at any given time without having to modify your code. This vastly improves app
performance and app reliability. In our case, we only have one CPU available but in your
own projects, you can decide accordingly. Also, if you wish to use a specific number of
CPU cores, say two. Then you would just enter the flag
So what about when we need to reload our app but we don't want any downtime? PM2 has us covered there as well.
pm2 reload MyApi
Again, that was easy and extremely useful when your app is running live and can't afford the downtime.
So what about all this monitoring we keep talking about? How do we do that? Again, this is easy.
This allows you to view the resource usage of all running process as well as any logs straight from the terminal. This is far more detailed than PM2 list.
So while there is much more to be explored in PM2, we will stop at one more command. This one is especially critical. We may be currently running our app as a daemon, but what happens when Digital Ocean (or any service provider) has to shutdown the server for routine maintenance? When that server restarts your app won't start. What we need is a startup script. Fear not though, PM2 also has this covered.
pm2 startup freebsd
That's it. PM2 has now initiated the startup scripts needed for your app to immediately start running on system startup.
Finally, let's jump to our web browser and go to Digital Ocean.
Now that we have gotten this far, we're going to use a little movie magic to create one more FreeBSD droplet that is configured the exact savme as Node1 only we will name it, you guessed it, Node2.
Bam, movie magic is great isn't it? So you may be wondering why we did this. This is because when we setup Nginx, we'll be using it as a load balancer to enhance the performance and stability of our system by proxying to the 2 APIs.
We now that we have our Node API droplets configured, in place, and ready to go. Tomorrow we will learn about Nginx then set it up as our reverse proxy and load balancer. We will also show how you can use it to setup HTTPS at the front to enhance security and protect against man-in-the-middle attacks.
http://pm2.keymetrics.io/docs/usage/quick-start/ http://pm2.keymetrics.io/docs/usage/cluster-mode/ http://pm2.keymetrics.io/docs/usage/startup/