0
votes

Summary

My node.js express server application works like a champ on Google App Engine when using web preview. However, when I try to deploy (manually, from the Google Cloud Console), the app no longer works. When I hit the URL, I get a "500 Error: Server Error The server encountered an error and could not complete your request. Please try again in 30 seconds." 30 seconds, 30 minutes, 30 hours later -- still the same error. Very puzzling, because the web app works fine on my local machine and also when I use web preview on GCP -- just when I try to do the final deployment.

Background

Runs perfectly on local machine. Code uploaded to my repo on git, then cloned into my app engine project, then npm installed, then npm started on Google App Engine (GAE). Web preview checks out just fine. Gcloud app deploy uploads the files to the zone without error and gives me the URL. That's when it doesn't work -- I get the 500 error. Looked at the error messages using 'gcloud app logs tail -s default.'

What I've tried:

  • Changed the express version number in package.json from 4.17 to 4.16 -- this did not resolve the error
  • Made sure the node version I used on my local machine was node 10 since that's what's on GAE (I was on 12 on my local machine) -- app continued to work flawlessly on local machine
  • Deleted original project and created another project to start from scratch and created the app -- still had problems when deploying (I think I may have switched regions/zones, but I'm not sure.) -- this did not resolve the error.

Code Section

1. app.js
const express = require('express');
const path = require('path');
/* Tutorial: https://www.youtube.com/watch?v=L72fhGm1tfE */

const server2 = express();

oneHundredTenDays = 60*1000*60*24*110; // cache (110 days)

// Set a static folder
server2.use(express.static(path.join(__dirname, 'public'), {maxAge: oneHundredTenDays}));

const PORT = process.env.PORT || 5006;

server2.listen(PORT, () => console.log(`Express server2 started on port ${PORT}`));

2. app.yaml -- redirects all http requests to https requests
handlers:
- url: /.*
script: auto
secure: always
redirect_http_response_code: 301

runtime: nodejs10

3. package.json
{
"name": "expressserver2",
"version": "1.0.0",
"description": "https://www.youtube.com/watch?v=L72fhGm1tfE",
"main": "app.js",
"scripts": {
   "startSecure": "node appHttps",
   "devSecure": "nodemon appHttps",
   "startUnsecure": "node app",
   "devUnsecure": "nodemon app"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
  "express": "^4.16.1"
},
"devDependencies": {
   "nodemon": "^1.19.1"
}
}

4. history of commands I used on Google Cloud Shell:
409  rm -rf expressServer2
410  git clone https://github.com/xxxxxxxxxx/expressServer2
411  cd expressServer2/
412  cp ../expressServer1/app.yaml .
413  export PORT=9088 && npm install
414  npm run startd
415  more package.json
416  npm run startUnsecure
417  gcloud app create
418  gcloud app deploy
419  gcloud app logs tail -s default

Error Log:

I expected to see a working web app (as I see when I do a web preview after command 416, above). Now, for the error I see after issuing command 419:

2019-09-11 21:04:50 default[20190911t170310]  Error: Cannot find module '/srv/server.js'      at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)      at Function.Module._load (internal/modules/cjs/loader.js:562:25)      at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)      at startup (internal/bootstrap/node.js:283:19)      at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
2019-09-11 21:04:50 default[20190911t170310]  "GET /favicon.ico HTTP/1.1" 500
2019-09-11 21:04:51 default[20190911t170310]  internal/modules/cjs/loader.js:638
2019-09-11 21:04:51 default[20190911t170310]      throw err;
2019-09-11 21:04:51 default[20190911t170310]      ^
2019-09-11 21:04:51 default[20190911t170310]
2019-09-11 21:04:51 default[20190911t170310]  Error: Cannot find module '/srv/server.js'      at Function.Module._resolveF

I think the favicon is red herring. The problem is that the application can't find '/srv/server.js'. I'm at a loss at this point. Is the Google deployment server environment really that different from the web preview version?

BTW: the page that it serves is not exactly static -- some simple javascript -- one text box input and some images, if you think any of that matters.

I'll pursue any leads recommended.

1

1 Answers

3
votes

Since you're experiencing

Error: Cannot find module '/srv/server.js'

try adding the following line to the “scripts” in your package.json file:

"start": "node app.js",

Keep in mind that, as Google App Engine creates a new instance for your application, the instance must first load the libraries and resources required to handle the request.

If modifying your package.json file alone doesn’t resolve your issue, the reason could be that you don't have any active instances that are ready to handle requests, which causes them to time out when App Engine loads your code into a new instance (see the issue described in this post).

If this is the case, you might want to reduce the duration of the loading requests. Alternatively, you can use warmup requests to keep your instances active even if there’s no traffic.

Let me know if it helps.