gpt4 book ai didi

javascript - 即使没有其他进程阻塞该端口,Node.js 应用程序也无法在端口 80 上运行

转载 作者:IT老高 更新时间:2023-10-28 12:24:36 25 4
gpt4 key购买 nike

我在安装了 Node.js 的 Amazon EC2 上运行 Debian 实例。如果我运行下面的代码:

http = require('http');

http.createServer(function (request, response){
response.writeHead(200, {'Content-Type':'text/plain'});
response.end('Hello World\n');
}).listen(80);
console.log("Running server at port 80");

我得到下面的输出,它告诉我在端口 80 上监听另一个进程:

Running server at port 80

events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EACCES
at errnoException (net.js:901:11)
at Server._listen2 (net.js:1020:19)
at listen (net.js:1061:10)
at Server.listen (net.js:1127:5)
at Object.<anonymous> (/home/admin/nodetests/nodetest.js:6:4)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)

现在,当我检查是否有进程(以 root 身份,以防有任何东西被隐藏)监听端口 80 时,使用:

netstat -tupln

我得到下面的输出,它告诉我端口 80 没有监听:

Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1667/sshd
tcp6 0 0 :::22 :::* LISTEN 1667/sshd

我应该注意到,如果这有所不同,debian 会将端口 80 作为入站规则打开。

我的问题是:我做错了什么?为什么我无法识别监听 80 端口的进程?为什么它在 Debian 中被阻止?我应该采取哪些步骤才能使代码正确运行?

最佳答案

错误代码 EACCES 表示您没有在该端口上运行应用程序的适当权限。在 Linux 系统上,任何低于 1024 的端口都需要 root 访问权限。

关于javascript - 即使没有其他进程阻塞该端口,Node.js 应用程序也无法在端口 80 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18947356/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com