在大多数端口上侦听时,Node.js EACCES错误

http Node.js

凯米亚

2020-03-23

我正在测试一个应用程序(希望可以在heroku上运行,但是在本地也有问题)。它在运行http.Server.listen()时给我一个EACCES错误-但它仅在某些端口上发生。

因此,我正在本地运行:

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)

我在900端口(或我尝试过的其他20个端口中的任何一个)上没有任何运行,因此这应该可以工作。奇怪的是它确实在某些端口工作。例如,端口3000可以正常工作。

是什么原因造成的?

更新1:

我发现在我的本地计算机上,EACCES错误来了,因为我必须以root用户身份运行节点才能绑定到这些特定端口。我不知道为什么会这样,但是使用sudo可以解决。但是,这并不能解释我将如何在Heroku上修复它。无法在Heroku上以root用户身份运行,那么如何在80端口监听?

第2580篇《在大多数端口上侦听时,Node.js EACCES错误》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

11个回答
老丝 2020.03.23

在尝试了许多不同的方法之后,在我的Windows上重新安装IIS解决了该问题。

番长猴子 2020.03.23

仅通过更改server.js中的端口号即可解决我的错误

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

我将端口号从8080更改为8085。

希望能帮助到你。

LGil 2020.03.23

尝试authbind:

http://manpages.ubuntu.com/manpages/hardy/man1/authbind.1.html

安装后,可以在以下文件夹中添加要使用端口号的文件:/ etc / authbind / byport /

使用chmod授予它500个权限,并将所有权更改为您要在其下运行程序的用户。

之后,以您的项目中的该用户身份执行“ authbind node ...”。

达蒙Tom 2020.03.23

我在Mac上收到此错误,因为默认情况下,它使用与节点服务器使用的端口相同的端口来运行apache服务器,在我的情况下,端口是端口80。我要做的就是用 sudo apachectl stop

希望这对某人有帮助。

小胖 2020.03.23

我的Mac上也出现了此错误。我使用npm run dev运行在Windows中我的应用程序的NodeJS和它工作正常。但是我的mac-上出现了此错误error given was: Error: bind EACCES null:80

解决此问题的一种方法是使用root用户访问权限运行它。您可能会使用sudo npm run dev并且需要输入密码。

通常最好在非特权端口(例如3000)上为您的应用程序提供服务,该端口无需root权限即可工作。

参考:在HTTP 80端口上侦听时,Node.js EACCES错误(权限被拒绝)

Mandy 2020.03.23

如果您尝试本地托管的端口被转发,则会发生这种情况

小胖 2020.03.23

请记住,如果您使用sudo绑定到端口80,并且正在使用env变量PORT&NODE_ENV,则必须重新导出这些var,因为您现在位于根配置文件而非用户配置文件下。因此,要使其在我的Mac上运行,我做了以下工作:

sudo su
export NODE_ENV=production
export PORT=80
docpad run
小宇宙 2020.03.23

我的天啊!!在我的情况下,我....listen(ip, port)不是这样做,而是...listen(port, ip)抛出错误味精:Error: listen EACCES localhost

我使用的端口号> = 3000,甚至尝试使用管理员访问权限。什么都没解决。然后仔细观察,我注意到了这个问题。更改为...listen(port, ip),一切开始正常!

只是大声说出来,以防它对其他人有用...

番长猴子 2020.03.23

这意味着节点无法在定义的端口上侦听。将其更改为1234或2000或3000,然后重新启动服务器。

斯丁 2020.03.23

Check this reference link:

Give Safe User Permission To Use Port 80

Remember, we do NOT want to run your applications as the root user, but there is a hitch: your safe user does not have permission to use the default HTTP port (80). You goal is to be able to publish a website that visitors can use by navigating to an easy to use URL like http://ip:port/

Unfortunately, unless you sign on as root, you’ll normally have to use a URL like http://ip:port - where port number > 1024.

A lot of people get stuck here, but the solution is easy. There a few options but this is the one I like. Type the following commands:

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

Now, when you tell a Node application that you want it to run on port 80, it will not complain.

泡芙猪猪 2020.03.23

非特权用户(非root用户)无法在1024以下的端口上打开侦听套接字。

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android