为什么我的应用程序 运行 无法在 Heroku 的预期端口上运行?

Why can't my app run on the intended port in Heroku?

我无法弄清楚 heroku 的问题是什么,我花了 2 天时间试图弄清楚这个错误意味着什么无济于事。

2021-07-18T04:27:08.741998+00:00 app[web.1]: {"level":30,"time":1626582428741,"pid":44,"hostname":"98a3475f-ac16-4dfa-91e0-46d53d3b5e4c","msg":"Server listening at http://127.0.0.1:34196"}

如您所见,服务器是 运行,但是当我尝试从网络访问我的应用程序时。发生这种情况..

2021-07-18T04:27:56.941807+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch

我不知道问题出在哪里。我的应用程序显然 运行 在 Heroku 指定的预期端口上。

package.json

"scripts": {
"test": "npm run build:ts && tsc -p test/tsconfig.test.json && cross-env TS_NODE_FILES=true tap --ts test/**/*.test.ts",
"start": "npm run build:ts && fastify start -l info dist/app.js",
"build:ts": "tsc",
"dev": "tsc && concurrently -k -p \"[{name}]\" -n \"TypeScript,App\" -c \"yellow.bold,cyan.bold\" \"tsc -w\" \"fastify start --ignore-watch=.ts$ -w -l info -P dist/app.js\"",
"database": "npx prisma migrate dev && npx prisma db seed --preview-feature" },

这是来自 heroku logs --tail

的日志
2021-07-19T04:34:45.411610+00:00 heroku[web.1]: Stopping process with SIGKILL
2021-07-19T04:34:45.535520+00:00 heroku[web.1]: Process exited with status 137
2021-07-19T04:34:45.641077+00:00 heroku[web.1]: State changed from starting to crashed
2021-07-19T04:34:45.648836+00:00 heroku[web.1]: State changed from crashed to starting
2021-07-19T04:34:59.197914+00:00 heroku[web.1]: Starting process with command `npm start`
2021-07-19T04:35:02.499184+00:00 app[web.1]:
2021-07-19T04:35:02.499211+00:00 app[web.1]: > app@1.0.0 start /app
2021-07-19T04:35:02.499211+00:00 app[web.1]: > fastify start -l info dist/app.js
2021-07-19T04:35:02.499212+00:00 app[web.1]:
2021-07-19T04:35:03.830676+00:00 app[web.1]: {"level":40,"time":1626669303829,"pid":21,"hostname":"5b062824-4105-40ff-87b7-5846163fdac5","msg":"Allowing all origins"}
2021-07-19T04:35:03.856136+00:00 app[web.1]: {"level":40,"time":1626669303855,"pid":21,"hostname":"5b062824-4105-40ff-87b7-5846163fdac5","msg":"\"root\" path \"/app/assets\" must exist"}
2021-07-19T04:35:05.924577+00:00 app[web.1]: {"level":30,"time":1626669305924,"pid":21,"hostname":"5b062824-4105-40ff-87b7-5846163fdac5","msg":"Server listening at http://127.0.0.1:42948"}
2021-07-19T04:35:59.304046+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2021-07-19T04:35:59.382557+00:00 heroku[web.1]: Stopping process with SIGKILL
2021-07-19T04:35:59.486461+00:00 heroku[web.1]: Process exited with status 137
2021-07-19T04:35:59.559137+00:00 heroku[web.1]: State changed from starting to crashed

注意应用程序 运行 很好,但过了一会儿 heroku 在应用程序启动后从 heroku[web.1] 指示停止工作,即 app[web.1]

您调用 tsc 并且构建需要时间。

build:ts 更改为 build 并删除 start 脚本中的 npm run build:ts

文件here

{
    "name": "something",
    "others key": "here",
    "scripts": {
        "start":"fastify start -l info dist/app.js",
        "build": "tsc",
        "others script":"echo 'others'",
    }
}

Heroku 只能接受 运行 在 0.0.0.0:{他们分配的任何端口} 上的应用程序。我的 Fasitfy 应用程序在 127.0.0.1 上 运行,这显然与 0.0.0.0 不同。尽管如此,Heroku 不接受除 0.0.0.0

以外的任何端口