# Deploy a Node.js application using an App Engine

In 
GCP
Published 2022-12-03

# App Engine short explanation

This tutorial explains how we can use GCP App Engine and how we can deploy a Node.js App Engine.

App Engine is a fully managed, serverless platform for developing and hosting web applications at scale.

The languages we can use for creating an App Engine are: Node.js, Java, Ruby, C#, Go, Python, and PHP.

When we decide to use App Engine, we can use it into a Standard Environment or into a Flexible Environment.

Here are the main differences of these two options:

Standard Environment Flexible Environment
Application instances run in a sandbox, using the runtime environment of a supported language listed below Application instances run within Docker containers on Compute Engine virtual machines (VM)
Instance startup time: Seconds Instance startup time: Minutes
Modifying the runtime: No Modifying the runtime: Yes (through Dockerfile)
Pricing: Based on instance hours Pricing: Based on usage of vCPU, memory, and persistent disks
Writing to local disk: yes, to /tmp directory Writing to local disk: Yes, ephemeral (disk initialized on each VM startup)
SSH debugging: No SSH debugging:Yes

More information you have here

Let's create a simple Node.js web application and after that let's deploy it on CGP using App Engine.

# Prerequisite

  • we need to have Node.js installed on the local machine
node -v

v18.14.0
  • Google Cloud CLI is installed on the local machine

Install Google Cloud CLI on Windows explains how we can install GCP CLI on Window.

# Create a simple Node.js application

I will create a directory on my local machine first: C:\node.js\examples\app-engine1.

Navigate to the folder in your terminal, and create a package.json file by running npm init.

cd C:\node.js\examples\app-engine1
npm init

And you will see something like this:

This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.

See `npm help init` for definitive documentation on these fields
and exactly what they do.

Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.

Press ^C at any time to quit.
package name: (app-engine1)
version: (1.0.0)
description: My App Engine application
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
About to write to C:\node.js\examples\app-engine1\package.json:

{
  "name": "app-engine1",
  "version": "1.0.0",
  "description": "My App Engine application",
  "main": "index.js",
  "scripts": {
  "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}


Is this OK? (yes)

Add Express as a dependency for this application by running npm install express:

npm install express

added 57 packages, and audited 58 packages in 3s

7 packages are looking for funding
run `npm fund` for details

found 0 vulnerabilities

Add a start script to your package.json file:

"scripts": {
  "start": "node my-app-engine-server.js"
}

Create the my-app-engine-server.js file in the application directory, with the following code:

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello from My App Engine!');
});

// Listen to the App Engine-specified port, or 8080 otherwise
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
  console.log(`Server listening on port ${PORT}...`);
});

Now your simple Web Server is ready to start locally:

cd C:\node.js\examples\app-engine1
npm start

And you will see something like this:

> app-engine1@1.0.0 start
> node my-app-engine-server.js

Server listening on port 8080...

Now, http://localhost:8080 will get you an HTML page as response.

When all works well, we can deploy the application to App Engine.

For doing this, we need to create the app.yaml file in the application directory:

runtime: nodejs18

This file must contain the runtime for this application.

Now it is time for deploying our application to GCP App Engine. From the application directory run the following command:

gcloud app deploy

On the CLI you will see something like this:

Services to deploy:

descriptor:                  [C:\node.js\examples\app-engine1\app.yaml]
source:                      [C:\node.js\examples\app-engine1]
target project:              [fluted-layout-376110]
target service:              [default]
target version:              [20230209t231401]
target url:                  [https://fluted-layout-376110.lm.r.appspot.com]
target service account:      [App Engine default service account]


Do you want to continue (Y/n)?  y

Beginning deployment of service [default]...
Created .gcloudignore file. See `gcloud topic gcloudignore` for details.
#============================================================#
#= Uploading 4 files to Google Cloud Storage                =#
#============================================================#
File upload done.
Updating service [default]...done.
Setting traffic split for service [default]...done.
Deployed service [default] to [https://fluted-layout-376110.lm.r.appspot.com]

You can stream logs from the command line by running:
  $ gcloud app logs tail -s default

To view your application in the web browser run:
  $ gcloud app browse


Updates are available for some Google Cloud CLI components.  To install them,
please run:
  $ gcloud components update

As per our logs, at https://fluted-layout-376110.lm.r.appspot.com we can see our web server deployed: