Deploy a Node.js server using Google Cloud Compute Engine
Getting started with a minimal Node.js “ping” app
This post shows how to deploy a minimal Express.js application to production from zero. It could be the starting point of any node project.
Minimum installation. Minimum code.
Skeleton code
I hate tutorials like “How to build <a-super-long-and-difficult-task> in only 5 minutes !” when I actually have to clone a repository and run npm start
.
OK, it works (sometimes it doesn’t).
But I didn’t learn anything.
Therefore, I provide the minimal Node.js skeleton to start with simple stuffs.
This server only returns “pong” when http://localhost:8080/api/v1/ping is called.
That’s all.
Check and clone the skeleton code from here.
sudo apt install git # if you don't have git installedgit clone https://github.com/BerangerNt/nodejs-express-skeleton.git
I assume you have Node.js and npm installed. If not, follow these instructions : https://www.npmjs.com/get-npm
Once cloned, in a terminal, run :
cd nodejs-express-skeletonnpm install
And :
npm run start
You should have this result : “Node app running at localhost:8080” :
Paste http://localhost:8080/api/v1/ping in a browser, “pong” should be displayed.
Again, no logic here, only a structure.
We now have a server running locally. It’s time to go online.
For this we need to create a virtual machine.
Create a Virtual Machine
Follow this link : https://console.cloud.google.com/compute/instances
Click on “Create Instance” and copy the following configurations to use a Ubuntu image (you might have to create a new project) :
The creation process should take about 10 minutes.
Install Node
To connect to your server directly from the browser, click on the SSH button on the right of your virtual machine :
A terminal should pop up :
For now, the server only contains an image of Debian 10, we need to install npm and node, run the following commands :
sudo apt-get -y updatesudo apt-get install -y nodejs npmsudo apt install git
Check the installation :
Clone and install the node skeleton code
Now run :
git clone https://github.com/BerangerNt/nodejs-express-skeleton.git
cd nodejs-express-skeleton/
npm install
npm run start
If you get the following screen, you’re on the right path :
Configure Nginx
Nginx will serve as our reverse proxy. This allows our node application to be accessed from port 80, run :
sudo apt-get install -y nginx
Nginx is installed, we now have to configure it for our node application :
Navigate to Nginx’s sites-available folder.
cd /etc/nginx/sites-available
And change the default file, paste the following piece of code (get YOUR_SERVER_IP_ADDRESS here) :
sudo vim default
And restart nginx :
sudo service nginx restart
Install PM2
PM2 is a famous process manager that will help us to start, monitor and restart our server in case of crash. We don’t have much logs on this sample app, but if you intend to create multiple routes, I strongly recommend PM2.
cd ~/nodejs-express-skeletonsudo su rootsudo npm install -g pm2
Once it’s installed, you can start your server :
pm2 start server.js
A satisfying table should be displayed
You can play with pm2 status, pm2 logs to see your server working, to display logs, etc.
Enjoy a cup of tea
The server is completely set, you can now check in your browser that you get an answer : http://SERVER_NAME/api/v1/ping
You can go even faster if you automatise deployment from Github push and launch more tests (also called Continuous Deployment Continuous Integration (or CI/CD)).
Check the next article of this series to automate deployment on this server :