GCP AppEngine nodejs 服务器遇到错误,无法完成您的请求

GCP AppEngine nodejs The server encountered an error and could not complete your request

我在 AppEngine 上托管了一个 NodeJs 后端,当我使用 Postman 在本地请求一些 url 时它运行良好但是当我在生产环境 (AppEngine) 中请求它们时它 returns 我以下 html 长时间(超时)后出错:

<html>

<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <title>500 Server Error</title>
</head>

<body text=#000000 bgcolor=#ffffff>
    <h1>Error: Server Error</h1>
    <h2>The server encountered an error and could not complete your request.<p>Please try again in 30 seconds.</h2>
    <h2></h2>
</body>

</html>

我还用一个最小的工作项目进行了测试,只是一个 index.js,代码如下:

const http = require('http');

const port = 8000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World');
});

server.listen(port, () => {
  console.log(`Server running at port ${port}`);
});

它也一样。

我的应用,yaml 是这个:

runtime: nodejs16
service: test
instance_class: F2
env: standard

我去AppEngine日志里看日志,错误是这个:

Process terminated because the request deadline was exceeded. Please ensure that your HTTP server is listening for requests on 0.0.0.0 and on the port defined by the PORT environment variable. (Error code 123)

我尝试修改 app.yaml,删除我的服务并重新创建它,用一个小项目进行测试,结果还是一样...

我不明白问题出在哪里。 有人知道问题出在哪里吗?

谢谢!

我对 gcp AppEngine 很陌生,但我认为 this article 可以提供帮助。 来自文档:

Importantly, on the last few lines, the code has the server listen to the port specified by the process.env.PORT variable. This is an environment variable set by the App Engine runtime - if your server does not listen to this port, it will not be able to receive requests.

所以尝试添加这个

const port = parseInt(process.env.PORT) || 8000;