arrow_back

Deploy Node.js Express Application in App Engine

Join Sign in

Deploy Node.js Express Application in App Engine

55 minutes 1 Credit

GSP028

Google Cloud selp-paced labs logo

Overview

Google App Engine applications are easy to create, easy to maintain, and easy to scale as your traffic and data storage needs change. With App Engine, there are no servers to maintain. You simply upload your application and it's ready to go.

App Engine applications automatically scale based on incoming traffic. load balancing, microservices, authorization, SQL and NoSQL databases, Memcache, traffic splitting, logging, search, versioning, roll out and roll backs, and security scanning are all supported natively and are highly customizable.

App Engine's environments, the Standard Environment and the Flexible environment , support a host of programming languages, including Java, Python, PHP, Node.js, Go, etc.. The two environments give users maximum flexibility in how their application behaves since each environment has certain strengths. Read The App Engine Environments for more information.

This tutorial uses the sample code from the Google Cloud Node.js Getting Started guide

You will learn how to connect to computing resources hosted on Google Cloud via the web. You will learn how to use Cloud Shell and the Cloud SDK gcloud command.

What you'll learn

  • How to create a Node.js Express application on Google App Engine.
  • How to update the code without taking the server down.

Setup and Requirements

Qwiklabs setup

Before you click the Start Lab button

Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.

This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.

To complete this lab, you need:

  • Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.
  • Time to complete the lab---remember, once you start, you cannot pause a lab.
Note: If you already have your own personal Google Cloud account or project, do not use it for this lab to avoid extra charges to your account.

How to start your lab and sign in to the Google Cloud Console

  1. Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:

    • The Open Google Console button
    • Time remaining
    • The temporary credentials that you must use for this lab
    • Other information, if needed, to step through this lab
  2. Click Open Google Console. The lab spins up resources, and then opens another tab that shows the Sign in page.

    Tip: Arrange the tabs in separate windows, side-by-side.

    Note: If you see the Choose an account dialog, click Use Another Account.
  3. If necessary, copy the Username from the Lab Details panel and paste it into the Sign in dialog. Click Next.

  4. Copy the Password from the Lab Details panel and paste it into the Welcome dialog. Click Next.

    Important: You must use the credentials from the left panel. Do not use your Google Cloud Skills Boost credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
  5. Click through the subsequent pages:

    • Accept the terms and conditions.
    • Do not add recovery options or two-factor authentication (because this is a temporary account).
    • Do not sign up for free trials.

After a few moments, the Cloud Console opens in this tab.

Note: You can view the menu with a list of Google Cloud Products and Services by clicking the Navigation menu at the top-left. Navigation menu icon

Activate Cloud Shell

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.

  1. In the Cloud Console, in the top right toolbar, click the Activate Cloud Shell button.

Cloud Shell icon

  1. Click Continue.

It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. The output contains a line that declares the PROJECT_ID for this session:

Your Cloud Platform project in this session is set to YOUR_PROJECT_ID

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

  1. (Optional) You can list the active account name with this command:

gcloud auth list

(Output)

ACTIVE: * ACCOUNT: student-01-xxxxxxxxxxxx@qwiklabs.net To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. (Optional) You can list the project ID with this command:

gcloud config list project

(Output)

[core] project = <project_ID>

(Example output)

[core] project = qwiklabs-gcp-44776a13dea667a6 For full documentation of gcloud, in Google Cloud, Cloud SDK documentation, see the gcloud command-line tool overview.

Enable the App Engine Admin API

Click on the Navigation menu icon in the top left of the screen:

burger_menu.png

Select APIs & services from the drop down and click on Enabled APIs and services:

apis_services.png

Click on Enable APIs and services:

enable_api.png

Then, search for App Engine in the search box.

Click on App Engine Admin API, then click Enable if the API isn't enabled already.

Get the Getting Started Example source code

After Cloud Shell launches, you can use the command line to clone the example source code in the home directory:

git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git && cd nodejs-docs-samples/appengine/hello-world/flexible

Adjust the version of node in your package.json:

sed -i 's/12.20.0/12.14.1/g' package.json

Now, install the dependencies with npm:

npm install

Run the Application Locally

You can start the Node.js application normally with npm:

npm start

Once the application started, click on the Web Preview icon web-preview-icon.pngin the Cloud Shell toolbar and choose preview on port 8080.

preview-8080.png

A tab in your browser opens and connects to the server you just started.

web_preview.png

In the cloud shell command line, press Ctrl+C to quit.

Deploying the Application into App Engine

App Engine uses a file called app.yaml to describe an application's deployment configuration. If this file is not present, App Engine will try to guess the deployment configuration. However, it is a good idea to provide this file.

Open app.yaml to see what it contains. You can use vim, nano,or emacs to view the file:

app.yaml

runtime: nodejs env: flex

This is the basic configuration needed to deploy a Node.js application. You can learn more about configuring App Engine here .

Close the file without making any modifications.

To deploy your application into App Engine environment, run:

gcloud app deploy

Because this is the first time you are deploying App Engine, the tool will prompt you to select a location to deploy the app. For this lab choose us-central and click Enter.

app_regions_output.png

When prompted, enter y to confirm deployment.

First time deployment may take several minutes. This is because App Engine Flexible environment automatically provisions a Compute Engine virtual machine for you behind the scenes, and then installs and starts the application.

After the application deploys, you can visit it by opening the URL http://<project-id>.appspot.com in your web browser. The project-id is the Project ID in the CONNECTION DETAILS section of the lab.

Alternatively, you enter the following command for the URL link:gcloud app browse

In this step, you set up a simple Node.js application and ran and deployed your application on App Engine.

Test Completed Task

Click Check my progress to verify your performed task. If you have successfully deployed the application into App Engine, you will see an assessment score.

Deploying the Application into App Engine

Updating the Application

Update the application to generate a UUID every time someone visits the page.

Install the uuid package with npm:

npm install uuid@^3.1 --save

Now modify app.js to use uuid. You can use vim, nano, or emacs to edit the file:

Enter the following in the the Cloud Shell Command Line to update the app.js file:

vi app.js

Start the editor:

i

Import the uuid module in app.js file:

const uuid = require('uuid/v4');

Update the following in app.js file:

.send(`Hello, ${uuid()}!`)

Save the app.js file by pressing: <Esc> then entering:

:wq

app.js

'use strict'; // [START gae_flex_quickstart] const express = require('express'); const uuid = require('uuid/v4'); const app = express(); app.get('/', (req, res) => { res .status(200) .send(`Hello, ${uuid()}!`) .end(); }); // Start the server const PORT = process.env.PORT || 8080; app.listen(PORT, () => { console.log(`App listening on port ${PORT}`); console.log('Press Ctrl+C to quit.'); }); // [END gae_flex_quickstart]

Now, you can test the application locally.

Start the Node.js application with npm:

npm start

Once the application started, click on the Web Preview icon web-preview-icon.pngin the Cloud Shell toolbar and choose preview on port 8080.

preview-8080.png

A tab in your browser opens and connects to the server you just started. You should see the new app that displays a UUID!

updated_web_preview.png

In the cloud shell command line, press Ctrl+C to quit.

To deploy your updated application, run:

gcloud app deploy

In a few minutes, the new version will be deployed and you can visit it by opening the URL http://<project-id>.appspot.com in your web browser.

Alternatively, you enter the following command for the URL link: gcloud app browse

In this step, you updated your Node.js application without any downtime.

Test Completed Task

Click Check my progress to verify your performed task. If you have successfully deployed the updated application into App Engine, you will see an assessment score.

Updating the Application

Congratulations!

You learned how to write and deploy your first App Engine web application!

completion_badge_Deploying_Applications-135.png

Finish Your Quest

This self-paced lab is part of the Deploying Applications Quest. A Quest is a series of related labs that form a learning path. Completing this Quest earns you the badge above, to recognize your achievement. You can make your badge (or badges) public and link to them in your online resume or social media account. Enroll in this Quest and get immediate completion credit if you've taken this lab. See other available Quests.

Take Your Next Lab

Continue your Quest with Build and Launch an ASP.NET Core App from Google Cloud Shell, or check out these suggestions:

Next Steps / Learn More

Google Cloud Training & Certification

...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.

Manual Last Updated March 17, 2022
Lab Last Tested March 17, 2022

Copyright 2022 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.