我正在测试一个应用程序(希望在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身份运行node才能绑定到那些特定的端口。我不知道为什么会发生这种情况,但是使用sudo可以修复它。然而,这并不能解释我如何在Heroku上修复它。没有办法在Heroku上以root身份运行,所以我怎么能在端口80上监听?


当前回答

在工作站上运行

作为一般规则,没有根权限的进程不能绑定到1024以下的端口。

所以尝试一个更高的端口,或者通过sudo以更高的权限运行。在使用进程绑定到低端口后,可以降级特权。Setgid和process.setuid。

在heroku上奔跑

当你在heroku上运行你的应用程序时,你必须使用port环境变量中指定的端口。

参见http://devcenter.heroku.com/articles/node-js

const server = require('http').createServer();
const port = process.env.PORT || 3000;

server.listen(port, () => console.log(`Listening on ${port}`));

其他回答

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

有时这是因为网点配置不好。例如:require(“dotenv”)。在你的应用程序中间件中配置没有(),或者可能是你用错误的语法写了你的端口号,比如你写了:而不是=,或者在端口号中添加了一些其他符号。

查看这个参考链接:

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.

#窗口

另一个原因——也许你的端口已经被某些原因排除在外了。

所以,尝试在管理员权限下打开CMD(命令行)并运行:

Net stop winnat Net start winnat

对我来说,这就足够了。

解决方案:https://medium.com/@Bartleby/ ports-the-- availal-listen-tcp -0-0-0-3000-165892441b9d

我尝试了上面给出的每个答案,但没有任何工作,然后我发现我忘记在.env文件中声明变量之前添加const。

之前:

PORT = 5000;
HOST = "127.0.0.1";

后:

const PORT = 5000;
const HOST = "127.0.0.1";