gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 03:01:56 31 4
gpt4 key购买 nike

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

我关注了Node.js getting started没有 heroku create 命令的部分,因为我已经从网络创建了。

因此,当我运行: heroku ps:scale web=1 时,它会显示:

Scaling dynos... done, now running web at 1:Free

但是当运行 heroku ps 时:

=== web (Free): npm run start (1)

web.1: crashed 2018/10/25 11:25:49 -0300 (~ 8m ago)

所以我在 heroku logs --tail 上的日志显示了这个错误:

2018-10-25T14:25:44.000000+00:00 app[api]: Build succeeded

2018-10-25T14:25:46.451739+00:00 heroku[web.1]: Starting process with command npm run start

2018-10-25T14:25:49.113832+00:00 app[web.1]:

2018-10-25T14:25:49.113864+00:00 app[web.1]: > my-app@1.0.0 start /app

2018-10-25T14:25:49.113866+00:00 app[web.1]: > node server.js

2018-10-25T14:25:49.113867+00:00 app[web.1]:

2018-10-25T14:25:49.418151+00:00 app[web.1]: events.js:167

2018-10-25T14:25:49.418191+00:00 app[web.1]: throw er; // Unhandled 'error' event

2018-10-25T14:25:49.418193+00:00 app[web.1]: ^

2018-10-25T14:25:49.418194+00:00 app[web.1]:

2018-10-25T14:25:49.418196+00:00 app[web.1]: Error: listen 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

我在日志中看到这一点:

remote: -----> Compressing...

remote: Done: 18.3M

remote: -----> Launching...

remote: Released v12

remote: https://my-app.herokuapp.com/ deployed to Heroku

remote:

remote: Verifying deploy... done.

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

3656da0..f1eb078 master -> master

那么...对我做错了什么有什么建议吗?提前致谢。

最佳答案

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

尝试使用这个:

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

Also you shouldn't bind the port to 80, as it is a reserved standard http port.

关于javascript - Heroku 应用程序 EACCES 上的 Nodejs Express 0.0.0.0 :80,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52992258/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com