如何使用sirv访问Heroku分配的端口?

时间:2019-12-02 08:17:44

标签: node.js heroku svelte

我遇到Heroku H20错误(应用程序启动超时),然后出现H10错误(Web进程在启动后60秒内未能绑定到$ PORT)

我的入门级瘦项目在package.json中使用sirv:

{
  "name": "clicker",
  "version": "0.0.1",
  "scripts": {
    "build": "rollup -c",
    "dev": "rollup -c -w",
    "start": "sirv public"
  },
  "engines": {
    "node": "10.16.0",
    "npm": "6.13.1"
  },
  "devDependencies": {
    "rollup": "^1.12.0",
    "rollup-plugin-commonjs": "^10.0.0",
    "rollup-plugin-livereload": "^1.0.0",
    "rollup-plugin-node-resolve": "^5.2.0",
    "rollup-plugin-svelte": "^5.0.3",
    "rollup-plugin-terser": "^5.1.2",
    "svelte": "^3.0.0"
  },
  "dependencies": {
    "sirv-cli": "^0.4.4"
  }
}

我似乎无法弄清楚如何通过$ PORT环境变量Heroku给您传递。我已经尝试过

"start": "sirv public --port $PORT"

The closest solution我发现这是指Heroku动态为您的应用分配端口的事实,但是我发现的所有解决方案都涉及在node中使用process.env.port,但我似乎无法弄清楚在我的package.json文件中说明如何与sirv结合使用。它基本上不是在命令行上运行“ sirv public”吗?因此,我不能仅在其中使用process.env.port,因为那是用于访问代码中的port变量...

Heroku日志

2019-12-02T07:54:26.210322+00:00 app[web.1]: Your application is ready~! �?
2019-12-02T07:54:26.210323+00:00 app[web.1]:
2019-12-02T07:54:26.210377+00:00 app[web.1]:   - Local:      http://localhost:19516
2019-12-02T07:54:26.210621+00:00 app[web.1]:
2019-12-02T07:54:26.210624+00:00 app[web.1]:  LOGS
2019-12-02T07:54:26.210625+00:00 app[web.1]:
2019-12-02T07:55:08.914191+00:00 heroku[router]: at=error code=H20 desc="App boot timeout"
2019-12-02T07:55:24.799059+00:00 heroku[web.1]: State changed from starting to crashed
2019-12-02T07:55:24.713988+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process failed to bind to $PORT within 60 seconds of launch
2019-12-02T07:55:24.714032+00:00 heroku[web.1]: Stopping process with SIGKILL
2019-12-02T07:55:24.782218+00:00 heroku[web.1]: Process exited with status 137
2019-12-02T08:02:13.343213+00:00 heroku[router]: at=error code=H10 desc="App crashed"

有什么想法吗?

2 个答案:

答案 0 :(得分:1)

使用内容创建一个.env文件(添加到.gitignore

HOST=localhost
PORT=5000

package.json启动命令:

{
  "start": "sirv public --host $HOST --port $PORT"
}

在本地运行以进行开发时执行:

heroku local

这将从.env提取您的环境变量

在生产中,Heroku将提供$HOST$PORT

$HOST可能可以用硬编码的0.0.0.0代替:

Why is my Node.js app crashing with an R10 error?

答案 1 :(得分:0)

By default, the server will only respond to requests from localhost. To allow connections from other computers, edit the sirv commands in package.json to include the option --host 0.0.0.0.

我将此添加到我的package.json中,它已成功完全安装在heroku上!