Shivam Singhal
Shivam Singhal

Reputation: 41

How to deploy Node.js express server on GCP whithout `gcloud app deploy`?

I do not want to use the command gcloud app deploy to deploy my Node.js Express server.

Ideally, I want to:

  1. Clone the repo on the VM Instance
  2. Run npm install
  3. Run npm start which will start the node server at port 5000.

What are the firewall rules for such configuration? Will I use the external IP of the VM to send the requests to my server or something else? What is the role, if any, of NGINX here?

Upvotes: 2

Views: 1619

Answers (2)

jviudes
jviudes

Reputation: 129

you could just config the regular http and https ports on Google cloud and use nginx as a proxy to route the data on to your app.

Nginx conf example:

server {
listen 80;

 location / {
     proxy_pass http://yourAppAddress:5000/;
 }
}

Although I would recommend using Docker to deploy your application.

Upvotes: 2

Wojtek_B
Wojtek_B

Reputation: 4443

To install and run Node.js express sercer on GCP instance follow these steps (tested on Debian9 VM):

sudo apt update
sudo su -
curl -sL https://deb.nodesource.com/setup_12.x | bash -
apt install -y nodejs
curl -L https://npmjs.org/install.sh | sudo sh
npm install -g express-generator
logout
express myproj1
cd myproj1
npm install
npm start

After which you should see

> [email protected] start /home/wbogacz/m1
> node ./bin/www

Regarding firewall - add a rule to allow TCP traffic on port 3000 to this machine; see example below;

gcloud compute --project=myproject firewall-rules create express_rule --direction=INGRESS --priority=1000 --network=default --action=ALLOW --rules=tcp:3000 --source-ranges=0.0.0.0/0 --target-tags=myvm

This assumes that your instance has a tag myvm.

After which you should be able to go to the external IP of your VM and see in the browser page with Welcome to Express message.

Upvotes: 1

Related Questions