我在 AppEngine 上托管了一個 NodeJs 后端,當我在本地使用 Postman 請求一些 url 時,它運行良好,但是當我在生產環境(AppEngine)中請求它們時,它在很長一段時間(超時)后回傳以下 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 日志中的日志,錯誤是這個:
由于超出了請求期限,行程終止。請確保您的 HTTP 服務器正在偵聽 0.0.0.0 和 PORT 環境變數定義的埠上的請求。(錯誤代碼 123)
我試圖修改 app.yaml,洗掉我的服務并重新創建它,用一個小專案進行測驗,結果仍然是一樣的......
我不明白問題出在哪里。有人知道問題出在哪里嗎?
謝謝 !
uj5u.com熱心網友回復:
我對 gcp AppEngine 很陌生,但我認為這篇文章可以提供幫助。從檔案中:
重要的是,在最后幾行,代碼讓服務器監聽由 process.env.PORT 變數指定的埠。這是 App Engine 運行時設定的環境變數 - 如果您的服務器不偵聽此埠,它將無法接收請求。
所以嘗試添加這個
const port = parseInt(process.env.PORT) || 8000;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/459121.html
