Deploying an Express API to Google App Engine

Todd H. Albert, Ph.D.
Boca Code
Published in
3 min readDec 11, 2021
Keep your users engaged with a fast-responding API hosted on Google App Engine

It costs money to drive users to your site or get users to download your app. So it’s critical that you retain as many of those users as possible. To do this, we need to assure that our websites and apps feel fast. If your app is sluggish, users will bounce. You can write the cleanest, fastest code for your site, but if your API is slow, nothing else matters.

So, what is the fastest way to host your API? From all of the methods we’ve tested, the fastest, and most scalable method to deploy your API also happens to be one of the easiest to setup and maintain. At Boca Code, we use Google App Engine for nearly all of our latest projects.

The basic steps are:

  1. Setup a Google Cloud Project. Enable Billing. Enable Cloud Build API.
  2. Install Google Cloud Command-Line Toolkit.
  3. Write a configuration file.
  4. Deploy.

Let’s break down each step.

First, let’s assume you already have an Express API written in JavaScript (I recommend using JavaScript for best compatibility; for TypeScript, you’ll have several more steps). In your Express setup, make sure you are allowing the PORT you are listening on to be defined via environmental variable. This is crucial.

The default port you listen on, here 8080, doesn’t matter, but you must include the first part.

Also, be sure that in your package.json file that you have a “start” script that runs your app.

Google will automatically run your start script to start your Express server. Be sure to include this in your package.json file.

You can create a Google Cloud Project from the Project Selector Dashboard. Be sure to enable billing for this project, even though most small projects will remain safely in Google’s Free Tier and never have any cost to run.

Next, enable the Cloud Build API for this project. This allows Google to automatically build your Express App before deploying.

If you haven’t setup the Google Cloud SDK on your computer before, you’ll need to install it before continuing.

Now you just add an app.yaml configuration file to the root of your project. YAML is a very simplified version of JSON where you don’t need quotes or braces, but where indentation matters. Use the following to create an App Engine instance that will automatically scale, always keep at least 1 engine running, and utilize the free tier.

Next we need to specify which project we want to deploy to. From command line in the project root, run gcloud config set project YOUR-PROJECT-ID

Now we are ready to deploy. Now run gcloud app deploy

This should now copy the files to Google Cloud and run Cloud Build. You can go to your Cloud Build dashboard and watch it install your node modules.

Once built, it will deploy an app engine instance and gracefully redirect traffic from any old instances to this new instance. You can watch this happen in the dashboard as well.

Google will automatically scale more instances of F1 (which is their Free Tier) as your traffic increases and scale back to 1 as traffic dies down.

To deploy a new version, just make changes and run gcloud app deploy

Troubleshooting

Often, when we install Google Cloud SDK, our computer has a hard time finding the gcloud binary. To resolve this, go into your ~/.zshrc or command line configuration file and add
export PATH=$HOME/google-cloud-sdk/bin:$PATH
or wherever you installed the SDK (here in my home folder/google-cloud-sdk, which is where they recommend).

Now open a new terminal window and try the command again. If it doesn’t work, just replace gcloud app deploy with ~/google-cloud-sdk/bin/gcloud app deploy

--

--

Todd H. Albert, Ph.D.
Boca Code

Software engineer; been mentoring founders, engineers, and students for 22+ years and building dozens of projects for startups to Fortune 100 companies.