Server error 500 when deploying NodeJS Express app on GCP App Engine

Viewed 1044

I'm trying to deploy a NodeJS app on gcloud that runs my express api. When I run npm start locally I get this message:

>npm start

> my_api@1.0.0 start E:\My_Project\My_API
> node index.js

Running API on port 8080
Connected to DB!

So I created an app.yaml file with the following properties:

runtime: nodejs12
entrypoint: npm start

manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10

Then I run gcloud app deploy, and everything is deployed properly to the correct project with no errors. But when I go to the base url, I get:

Error: Server Error

The server encountered an error and could not complete your request.

Please try again in 30 seconds.

So I checked the logs with gcloud app logs tail -s default, and I'm noticing the app keeps restarting over and over endlessly:

2020-12-08 00:10:21 default[...]  > node index.js
2020-12-08 00:10:21 default[...]
2020-12-08 00:10:21 default[...]  Running API on port 8081
2020-12-08 00:10:29 default[...]  "GET /_ah/start HTTP/1.1" 302
2020-12-08 00:10:31 default[...]
2020-12-08 00:10:31 default[...]  > my_api@1.0.0 start /workspace
2020-12-08 00:10:31 default[...]  > node index.js
2020-12-08 00:10:31 default[...]
2020-12-08 00:10:31 default[...]  Running API on port 8081
2020-12-08 00:10:41 default[...]  "GET /_ah/start HTTP/1.1" 302
2020-12-08 00:10:43 default[...]
2020-12-08 00:10:43 default[...]  > my_api@1.0.0 start /workspace
2020-12-08 00:10:43 default[...]  > node index.js
2020-12-08 00:10:43 default[...]
2020-12-08 00:10:43 default[...]  Running API on port 8081
2020-12-08 00:10:59 default[...]  "GET /_ah/start HTTP/1.1" 302
...

If I try a specific endpoint, I get these outputs (for one call):

2020-12-08 01:05:57 default[...]  "GET /my_api/some_endpoint HTTP/1.1" 500
2020-12-08 01:05:58 default[...]  "GET /favicon.ico HTTP/1.1" 500 <-- not sure what this is about

Also, I noticed that unlike in my local run, the gcloud app never outputs "Connected to DB!" which happens upon importing my database module into the routes.js file. Besides the app.yaml though, there's nothing different from running the app locally and on app engine, I'm just not sure why the app keeps restarting over and over every few seconds.

EDIT: So I noticed that, by default, app engine queries /_ah/start and /favicon.ico upon starting up the api, but since I don't have those endpoints coded, it errors and restarts. Is there any way to remove those two, or are they necessary for app engine to determine the app is working? OR at least I could add a /favicon.ico and /_ah/start endpoints, but what is the response format supposed to look like for it to respond? (I tried just returning 200 for both, but still get the same behavior)

2 Answers

Please read the following documentations

Its always better to read the documentation. Fix the following issues

Issue 1

Fix the following

2020-12-08 00:10:29 default[...]  "GET /_ah/start HTTP/1.1" 302

Quoting the docs

When an instance responds to the /_ah/start request with an HTTP status code of 200–299 or 404, it is considered to have successfully started and can handle additional requests.

Google App Engine uses a Unix socket to connect to a Cloud SQL instance, because of that you need to pass the instance's connection name, like in the example below:

var con = mysql.createConnection({
  host: "localhost",
  socketPath: "/cloudsql/<PROJECT_ID>:<REGION>:<SQL_INSTANCE_NAME>",
  user: "root",
  password: "secret"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});

In case you are using Google App Engine Flex, it is also important to set the correct beta_settings on your app.yaml like in the example below:

beta_settings:
  # The connection name of your instance, available by using
  # 'gcloud beta sql instances describe [INSTANCE_NAME]' or from
  # the Instance details page in the Google Cloud Platform Console.
  cloud_sql_instances: YOUR_INSTANCE_CONNECTION_NAME
Related