将 Google App Engine 应用程序部署到 GCP 时出现问题

Problem Deploying Google App Engine App to GCP

总结

我的 node.js Express 服务器应用程序在使用 Web 预览时在 Google App Engine 上表现出色。但是,当我尝试部署(从 Google Cloud Console 手动部署)时,应用程序不再运行。当我点击 URL 时,我收到“500 错误:服务器错误服务器遇到错误,无法完成您的请求。请在 30 秒后重试。” 30秒、30 分钟、30 小时后——仍然是同样的错误。非常令人费解,因为网络应用程序在我的本地机器上运行良好,而且当我在 GCP 上使用网络预览时也能正常运行——就在我尝试进行最终部署时。

背景

在本地机器上完美运行。代码上传到我在 git 上的存储库,然后克隆到我的应用程序引擎项目中,然后安装 npm,然后在 Google App Engine (GAE) 上启动 npm。网络预览检查得很好。 Gcloud app deploy 将文件上传到区域,没有错误,并给我 URL。那是它不起作用的时候——我收到 500 错误。使用 'gcloud app logs tail -s default.'

查看错误消息

我尝试过的:

代码部分

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 -- 将所有http请求重定向到https请求
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. 我在 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

错误日志:

我希望看到一个可用的网络应用程序(正如我在上面的命令 416 后进行网络预览时看到的那样)。现在,对于我在发出命令 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

我认为网站图标是红鲱鱼。问题是应用程序找不到'/srv/server.js'。在这一点上我不知所措。 Google 部署服务器环境真的与 Web 预览版有很大不同吗?

顺便说一句:它提供的页面并不是完全静态的 -- 一些简单的 javascript -- 一个文本框输入和一些图像,如果您认为其中任何一个很重要的话。

我会寻求任何推荐的线索。

因为你正在经历

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

尝试将以下行添加到 package.json 文件中的 “scripts”

"start": "node app.js",

请记住,当 Google App Engine 为您的应用程序创建一个 新实例 时,该实例必须首先加载处理请求所需的库和资源。

如果仅修改 package.json 文件不能解决您的问题,原因可能是您没有任何 活动实例 已准备好处理请求,这会导致它们在 App Engine 将您的代码加载到新实例 时超时(请参阅此中描述的问题post).

如果是这种情况,您可能希望 reduce the duration of the loading requests. Alternatively, you can use warmup requests 保持您的实例处于活动状态,即使没有流量。

如果有帮助请告诉我。