javascript - 在 GCP App Engine 上部署 NodeJS Express 应用程序时出现服务器错误 500

标签 javascript node.js express

我正在尝试在运行我的 express api 的 gcloud 上部署一个 NodeJS 应用程序。当我在本地运行 npm start 时,我收到以下消息:

>npm start

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

Running API on port 8080
Connected to DB!
所以我创建了一个具有以下属性的 app.yaml 文件:
runtime: nodejs12
entrypoint: npm start

manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 0.5
  disk_size_gb: 10
然后我运行 gcloud app deploy,一切都正确部署到正确的项目中,没有错误。但是当我转到基本网址时,我得到:
Error: Server Error

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

Please try again in 30 seconds.
所以我用 gcloud app logs tail -s default 检查了日志,并且我注意到该应用程序不断地不断重启:
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
...
如果我尝试一个特定的端点,我会得到这些输出(一次调用):
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
此外,我注意到与本地运行不同,gcloud 应用程序从不输出“已连接到数据库!”这发生在将我的数据库模块导入到 routes.js 文件中。除了 app.yaml 之外,与在本地和在应用程序引擎上运行应用程序没有什么不同,我只是不确定为什么应用程序每隔几秒钟就会不断重启。
编辑:所以我注意到,默认情况下,应用引擎会在启动 api 时查询/_ah/start 和/favicon.ico,但由于我没有对这些端点进行编码,它会出错并重新启动。有什么方法可以删除这两个,或者应用引擎是否需要它们来确定应用正在运行?或者至少我可以添加/favicon.ico 和/_ah/start 端点,但是响应格式应该是什么样子才能响应? (我尝试只为两者返回 200,但仍然得到相同的行为)

最佳答案

请阅读以下文档

  • https://cloud.google.com/appengine/docs/standard/nodejs/how-instances-are-managed
  • https://cloud.google.com/appengine/docs/standard/nodejs/runtime
  • https://cloud.google.com/appengine/docs/standard/nodejs/config/appref

  • 阅读文档总是更好。修复以下问题
    第一期
    修复以下问题
    2020-12-08 00:10:29 default[...]  "GET /_ah/start HTTP/1.1" 302
    
    引用 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.

    关于javascript - 在 GCP App Engine 上部署 NodeJS Express 应用程序时出现服务器错误 500,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65191673/

    相关文章:

    javascript - 基于动态 bool Angular 显示div

    node.js - Node.js 流如何工作?

    javascript - Socket.io:如果在快速路线内使用,事件会触发两次

    node.js - 如何从nodejs接收reactjs上的cookie

    javascript - 超测使用 express app 返回 301。应用程序只需要托管服务器

    javascript - 验证当月内的日期

    javascript - 显示与从 D3 图例中的范围映射的每个颜色渐变相关的值

    javascript悬停一个元素以更改另一个元素的文本颜色

    node.js - Sequelize 关联方法不起作用

    node.js - MongoDB:对 2 个集合的原子调用(查找+插入)