我正在测试一个应用程序(希望在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 个端口)上没有运行任何东西,所以这应该可以工作。奇怪的是它does在某些端口上工作。例如,端口 3000 运行良好。
什么会导致这种情况呢?
更新1:
我发现在我的本地计算机上,出现 EACCES 错误是因为我必须以 root 身份运行节点才能绑定到这些特定端口。我不知道为什么会发生这种情况,但使用 sudo 可以修复它。然而,这并没有解释我如何在 Heroku 上修复它。 Heroku 上无法以 root 身份运行,那么如何监听 80 端口呢?
在您的工作站上运行
作为一般规则,没有 root 权限运行的进程不能绑定到低于 1024 的端口。
因此,请尝试更高的端口,或者通过以下方式以提升的权限运行sudo
。绑定低端口后,可以使用以下命令降级权限process.setgid http://nodejs.org/api/process.html#process_process_setgid_id and process.setuid http://nodejs.org/api/process.html#process_process_setuid_id.
在 Heroku 上运行
在 heroku 上运行应用程序时,您必须使用 PORT 环境变量中指定的端口。
See http://devcenter.heroku.com/articles/node-js 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}`));
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)