'Nodejs Express on Heroku App EACCES 0.0.0.0:80

I am trying to run a Node app on a newly created Heroku App from their web. I followed their steps and still I am getting errors when displaying app status.

I followed the Node.js getting started section without the heroku create command since I already created from the web.

So when I run: heroku ps:scale web=1 it shows me:

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

But when running heroku ps:

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

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

So my logs on heroku logs --tail show me this error:

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]: > [email protected] 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

So I checked if I made a mistake while setting all up.

I use a simple Express routing and server with this code:

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

Also I made sure I added engines to package.json:

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

I've also created a Procfile file in the root path with web: npm run start inside.

So when everything is checked I just run these commands and everything looks great until I check the logs or visit the app:

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

I see this in the logs:

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

So... Any suggestions on what am I doing wrong? Thanks in advance.



Solution 1:[1]

You need to provide port for app to listen on in environment variable.
What heroku does is it runs our app on dynamic port.

Try using this:

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.

Solution 2:[2]

Heroku dynos expose a dynamic port for your app to bind to. This value is exposed in the $PORT env var. You must change your code to bind to this port instead.

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

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 AkshayM
Solution 2