11

The following error is common for people trying to run a Node.js server on port 80.

Error: listen EACCES 0.0.0.0:80

I used to solve this on my Amazon EC2 server simply by using

sudo node app.js

Now I've learned not to use that method for security concerns. A good solution as explained in this answer is to use:

sudo apt-get install libcap2-bin
sudo setcap cap_net_bind_service=+ep `readlink -f \`which node\``

However I'm not sure how to implement either solution on an AWS Elastic Beanstalk instance, where I don't seem to have SSH access the way I did for the AWS EC2 server, and the only way I seem to have for running anything is in my package.json file like this:

{
  "scripts": {
    "start": "node init"
  }
}

So I have no idea how I would run other types of commands. How is this done?

Community
  • 1
  • 1
curiosity5678
  • 155
  • 1
  • 6

3 Answers3

19

Beanstalk has a proxy build into it listening on port 80. Your Node.js app should only listen on process.env.PORT. Once it's done that, you'll be good to go.

Brad
  • 159,648
  • 54
  • 349
  • 530
  • and who sets `process.env.PORT`? – CodyBugstein Apr 09 '18 at 07:05
  • I am not sure if it has been changed but in my case even I set the port number to process.env.PORT it returns 8081 for https rather than 443 – Zhang Zhan Feb 28 '20 at 22:15
  • @CodyBugstein The Beanstalk system sets that. – Brad Feb 28 '20 at 22:26
  • @ZhangZhan Yeah, you shouldn't expect :443... Beanstalk uses its own proxy and what not internally. – Brad Feb 28 '20 at 22:26
  • @Brad But the problem is if I use process.env.PORT it redirect the browser to port 8081 which is not ideal as it will be displayed in the browser address and also not firewall friendly. – Zhang Zhan Feb 29 '20 at 04:13
  • @ZhangZhan No, it won't. That will only happen if your code does this redirect... which you shouldn't do. This is how Beanstalk works. There is a proxy set up internally. Externally, port :443 is used, and your users don't need to know or care that Beanstalk does something else internally. – Brad Feb 29 '20 at 04:18
  • @Brad you are correct. I did the redirection in my code and that is why it is going to 8081. Thank you. – Zhang Zhan Feb 29 '20 at 05:19
  • Here's the link for the documentation: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/nodejs-platform-proxy.html – Evandro Pomatti Sep 10 '22 at 19:23
1

Elastic beanstalk will route requests to your instances (at port 80 by default) from its load balancer. So in case you want to expose a particular port, you can add an entry to the load balancer listeners with the load balancer port as your desired port and instance port as 80.
Hope this helps. Can elaborate further if needed.

Shubhankar S
  • 460
  • 2
  • 8
1

For the

"scripts": {
    "start": "node init"
  }

You may use below to specify your running code is app.js

"scripts": {
    "start": "node app.js"
  }

In the app.js, if you use express.js

const port = process.env.port || 3000;
app.listen(port, () => {
    console.log("Sever console log.")
});

Then you have a add a parameter in Elastic Beanstalk > Select your Environment > Configuration > Edit the software configuration > add a Paramerter > Name: port Value: 8080

Rico Chan
  • 2,226
  • 2
  • 25
  • 32