I'm testing out an app (hopefully to run on heroku, but am having issues locally as well). It's giving me an EACCES error when it runs http.Server.listen() - but it only occurs on some ports.
So, locally I'm running:
joe@joebuntu:~$ node
> var h = require('http').createServer();
> h.listen(900);
Error: EACCES, Permission denied
at Server._doListen (net.js:1062:5)
at net.js:1033:14
at Object.lookup (dns.js:132:45)
at Server.listen (net.js:1027:20)
at [object Context]:1:3
at Interface.<anonymous> (repl.js:150:22)
at Interface.emit (events.js:42:17)
at Interface._onLine (readline.js:132:10)
at Interface._line (readline.js:387:8)
at Interface._ttyWrite (readline.js:564:14)
I don't have anything running on port 900 (or any of the other 20 ports I've tried), so this should work. The weird part is that it does work on some ports. For instance, port 3000 works perfectly.
What would cause this?
Update 1:
I figured out that on my local computer, the EACCES error is coming because I have to run node as root in order to bind to those certain ports. I don't know why this happens, but using sudo fixes it. However, this doesn't explain how I would fix it on Heroku. There is no way to run as root on Heroku, so how can I listen on port 80?
Another approach is to make port redirection:
And run your server on >1024 port:
ps the same could be done for https(443) port by the way.
I got this error on my mac because it ran the apache server by default using the same port as the one used by the node server which in my case was the port 80. All I had to do is stop it with
sudo apachectl stop
Hope this helps someone.
Try authbind:
http://manpages.ubuntu.com/manpages/hardy/man1/authbind.1.html
After installing, you can add a file with the name of the port number you want to use in the following folder: /etc/authbind/byport/
Give it 500 permissions using chmod and change the ownership to the user you want to run the program under.
After that, do "authbind node ..." as that user in your project.
Remember if you use sudo to bind to port 80 and are using the env variables PORT & NODE_ENV you must reexport those vars as you are now under root profile and not your user profile. So, to get this to work on my Mac i did the following:
Non-privileged user (not root) can't open a listening socket on ports below 1024.
Check this reference link: