侦听大多数端口时出现Node.js EACCES错误 [英] Node.js EACCES error when listening on most ports

查看:652
本文介绍了侦听大多数端口时出现Node.js EACCES错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

我没有在端口900(或我尝试过的其他20个端口)上运行任何东西,所以这应该有效。奇怪的是, 在某些端口上工作。例如,端口3000工作正常。

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.

这会导致什么?

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

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?

推荐答案

在工作站上运行



作为一般规则,没有root权限运行的进程无法绑定到1024以下的端口。

Running on your workstation

As a general rule processes running without root privileges cannot bind to ports below 1024.

所以尝试更高端口,或通过 sudo 以提升的权限运行。使用 process.setgid <绑定到低端口后,可以降级权限/ code> process.setuid

So try a higher port, or run with elevated privileges via sudo. You can downgrade privileges after you have bound to the low port using process.setgid and process.setuid.

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

When running your apps on heroku you have to use the port as specified in the PORT environment variable.

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

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

server.listen(port, function() {
  console.log('Listening on ' + port);
});

这篇关于侦听大多数端口时出现Node.js EACCES错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆