Heroku 应用程序上的 Nodejs Express EACCES 0.0.0.0:80

问题描述 投票:0回答:3

我正在尝试在他们的网络上新创建的 Heroku 应用程序上运行 Node 应用程序。我按照他们的步骤操作,但在显示应用程序状态时仍然遇到错误。

我遵循了 Node.js 入门部分,没有使用

heroku create
命令,因为我已经从网络创建了。

所以当我跑步时:

heroku ps:scale web=1
它向我显示:

缩放测功机...完成,现在以 1:Free 运行网络

但是跑步时

heroku ps
:

=== 网络(免费):npm run start (1)

web.1:崩溃 2018/10/25 11:25:49 -0300(~ 8m 前)

所以我的日志

heroku logs --tail
向我显示了这个错误:

2018-10-25T14:25:44.000000+00:00 app[api]: 构建成功

2018-10-25T14:25:46.451739+00:00 heroku[web.1]:使用命令启动进程

npm run start

2018-10-25T14:25:49.113832+00:00 应用程序[web.1]:

2018-10-25T14:25:49.113864+00:00 应用程序[web.1]: > [电子邮件受保护] 启动 /app

2018-10-25T14:25:49.113866+00:00 应用程序[web.1]:>节点server.js

2018-10-25T14:25:49.113867+00:00 应用程序[web.1]:

2018-10-25T14:25:49.418151+00:00 应用程序[web.1]: events.js:167

2018-10-25T14:25:49.418191+00:00 app[web.1]: 抛出错误; // 未处理的“错误”事件

2018-10-25T14:25:49.418193+00:00 应用程序[web.1]: ^

2018-10-25T14:25:49.418194+00:00 应用程序[web.1]:

2018-10-25T14:25:49.418196 + 00:00应用程序[web.1]:错误:听EACCES 0.0.0.0:80

所以我检查了一下我在设置时是否犯了错误。

我使用简单的 Express 路由和服务器以及以下代码:

app.get('/', (req, res) => { ... });
app.listen(80, err => {
    if(err) throw err;
    console.log("%c Server running", "color: green");
});

我还确保将

engines
添加到 package.json:

"scripts": {
    "start": "node server.js"
},
"engines": {
    "node": "10.11.0",
    "npm": "6.4.1"
},

我还在根路径中创建了一个 Procfile 文件,其中包含

web: npm run start

因此,当检查完所有内容后,我只需运行这些命令,一切看起来都很棒,直到我检查日志或访问应用程序:

git commit -am "my commit text"
git push heroku master

我在日志中看到这一点:

远程:----->正在压缩...

远程:完成:18.3M

远程:-----> 正在启动...

远程:发布 v12

远程:https://my-app.herokuapp.com/部署到Heroku

远程:

远程:验证部署...完成。

https://git.heroku.com/my-app.git

3656da0..f1eb078 主控 -> 主控

所以...对我做错了什么有什么建议吗?预先感谢。

javascript node.js git express heroku
3个回答
19
投票

您需要在环境变量中提供应用程序监听的端口。
Heroku 的作用是在动态端口上运行我们的应用程序。

尝试使用这个:

const PORT = process.env.PORT || 3000;
app.listen(PORT, err => {
    if(err) throw err;
    console.log("%c Server running", "color: green");
});

也不应该将端口绑定到 80,因为它是保留标准 http 端口。


0
投票

Heroku dynos 公开一个动态端口供您的应用程序绑定。该值在 $PORT 环境变量中公开。您必须更改代码才能绑定到此端口。

const port = process.env.PORT || 3000;

app.listen(port, err => {
    if(err) throw err;
    console.log("%c Server running", "color: green");
});

0
投票

对于模块解决方案(package.json 中的“type”:“module”):

const process = require('process');
const port = process.env.PORT || 8080;
© www.soinside.com 2019 - 2024. All rights reserved.