Open In App

Deploy & Run a Node.js Web Application on App Engine

Last Updated : 08 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Google Cloud Platform is one of the cloud service providers that provide various cloud services for a seamless experience. It provides various services like storage, networks, development tools, Analytics tools, infrastructure, and many more. To learn more about GCP you can follow this article

Terminologies

  • App Engine: This is a platform as a service of GCP that provides a fully managed serverless platform for application deployment.
  • NodeJS: NodeJS is a runtime javascript environment. It executes JavaScript code outside the browser.

How to deploy a web application on GCP?

  • We are going to use a simple NodeJS application that shows “Hello World” as a response. Below is an example of an app.js file.

Javascript




const express = require('express');
const app = express();
const port = 8000;
  
app.get('/',(req,res)=>{
    res.send("Hello World From GFG");
})
  
app.listen(port,()=>{
    console.log("Server Started On "+port);
})


  • You must install Express using npm. All the required dependencies must be installed. You can install Express using the below command inside the same folder as the app.js file.
npm install express
  • We are not deploying in production so we are not using environment variables here . If you want to deploy as production build then make sure you are using GCP secret manager and using proper environment variables.
  • Lets test the application in local enviroment . For starting the server we will add custom start script in package.json file . Add following lines to package.json file . The file should look like this . The start script will start the server by executing app.js file .

Node

{
“dependencies”: {
“express”: “^4.18.2”
},
“scripts”: {
“start”: “node app.js”
}
}

  • Run below command to start the server locally .
npm start
  • You should see below output in terminal .

Nodejs-run-output

  • Go to browser and hit 127.0.0.1:8000 . If you see “Hello World From GFG” then great the application is setup correctly we can deploy it.
  • Now for deploying firstly download and install google cloud sdk on your machine . After successful installation you can verify by running “gcloud version” command.
  • Google App Engine uses app.yaml file for configuring app engine instance so lets create app.yaml in folder where app.js file is located. Insert following code in app.yaml file.
runtime : nodejs18
  • Now initialize gcloud CLI if not already initialized by below command.
gcloud init
  • select your project after logging in or create a new one.
  • Now create a new app in app engine using below command . select region for application creation. This will create a new app in specified region.
gcloud app create
  • Run below command where app.yaml file is located which will deploy our NodeJS application .
gcloud app deploy
  • Review details press Y to continue . After successful deployment you will see below output .

app-deploy-output

  • Now to go to browser and go to service url of application OR run below command .
gcloud app browse

app-browse-output

  • This will open browser with app URL. If you see “Hello World From GFG” then congratulations your app is deployed successfully.

browser-output

Troubleshooting A Deployed Web Application On GCP

  • If you get ‘502 Bad Gateway’ error go to logs explorer to view what is wrong .
  • If there is any other error check your project for configurations and make necessary changes.
  • If you still cannot access the page check the firewall rules to check if there is blocking rule to the network which is blocking the traffic.
  • If you are getting 404 Not Found then check the routes that you are accessing . Check that the route you are accessing exist in the server.

Conclusion

From this article we have learnt about deployment of NodeJS web applications on google cloud platform. We have also seen step by step process to deploy an web application.

Frequently Asked Questions (FAQs)

1. What are other options to deploy a NodeJS web application in GCP ?

There are options like Cloud Run , Cloud functions and Instances where you can deploy the NodeJS Application.

2. Why App Engine is better option over other services in GCP for deploying NodeJS web application ?

App engine is fully managed platform where you just have to provide your code . All other things will be taken care by GCP hence it is a better option over other services.

3. How to update NodeJs application and redeploy on app engine ?

Update your code of the application on your machine and simply run deploy command for app engine which will redeploy the running application and update it to the newest version.

4. What is difference in App Engine and Cloud Run ?

The main difference between them is that Cloud Run is designed for teams that include both software developers and ops experts, while App Engine is designed for developers1. Cloud Run is a newer service that only runs when it is serving requests, while App Engine has been around for longer and has automatic scaling



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads