Nodejs能够监听80端口吗?

Nodejs能够监听80端口吗?

我是一个刚刚接触nodejs的菜鸟,今天在学着写一个程序的时候

var http = require("http");
var url = require("url");
function start(){
	console.log("call the start");
	function onRequest(request,response){
		console.log("call the onRequest function");
		response.writeHead(200,{"Content-type":"text/plain"});
		response.write("aa");
		response.end();
	}
	http.createServer(onRequest).listen(80);
	console.log("Server has started.");
}
exports.start = start;

发现会控制台会报错

call the start
Server has started.

events.js:66
        throw arguments[1]; // Unhandled 'error' event
                       ^
Error: listen EACCES
    at errnoException (net.js:768:11)
    at Server._listen2 (net.js:891:19)
    at listen (net.js:935:10)
    at Server.listen (net.js:984:5)
    at Object.start (/home/xing/ANodeJsWebSite/node/server.js:11:31)
    at Object.<anonymous> (/home/xing/ANodeJsWebSite/node/index.js:2:8)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)

而把listen的端口改成8888之后就可以成功运行了。

想请问一下各位大侠,这是为什么


18 回复

当然可以。标题为“Nodejs能够监听80端口吗?”的帖子内容描述了一个常见的问题,即在使用Node.js监听80端口时遇到权限问题。让我们通过分析代码和解释原因来解答这个问题。

问题原因

80端口是HTTP协议的默认端口,通常被系统服务或受信任的应用程序所占用。因此,普通用户(非root用户)在尝试绑定到80端口时,会因为权限不足而失败。具体错误信息Error: listen EACCES表示没有足够的权限访问该端口。

示例代码解析

你提供的代码展示了如何创建一个简单的HTTP服务器并监听80端口:

var http = require("http");
var url = require("url");

function start() {
    console.log("call the start");

    function onRequest(request, response) {
        console.log("call the onRequest function");
        response.writeHead(200, {"Content-type": "text/plain"});
        response.write("aa");
        response.end();
    }

    http.createServer(onRequest).listen(80);
    console.log("Server has started.");
}

exports.start = start;

在这段代码中:

  • http.createServer(onRequest) 创建一个新的HTTP服务器,并指定当接收到请求时调用onRequest函数。
  • .listen(80) 方法尝试让服务器监听80端口。

由于80端口需要管理员权限,所以这段代码在非root用户下运行时会失败。

解决方案

解决方法有两种:

  1. 以管理员身份运行:使用sudo命令运行你的Node.js脚本,例如:

    sudo node your_script.js
    
  2. 使用非特权端口:选择一个大于1024的端口,如8888,这样不需要管理员权限:

    http.createServer(onRequest).listen(8888);
    

示例代码修改

将端口从80更改为8888,代码如下:

var http = require("http");
var url = require("url");

function start() {
    console.log("call the start");

    function onRequest(request, response) {
        console.log("call the onRequest function");
        response.writeHead(200, {"Content-type": "text/plain"});
        response.write("aa");
        response.end();
    }

    http.createServer(onRequest).listen(8888);  // 修改为8888端口
    console.log("Server has started.");
}

exports.start = start;

通过上述修改,你可以避免权限问题,并且服务器可以正常启动和响应请求。

希望这些解释能帮助你理解为什么80端口需要管理员权限以及如何解决这一问题。


端口被占用或者是没有权限吧 EACCES应该是没有权限 Non-privileged user (not root) can’t open a listening socket on ports below 1024.

端口冲突吧,查看下你的80端口有没有在使用

Non-privileged user (not root) can’t open a listening socket on ports below 1024.

看到这个没权限,我试着使用sudo node index.js,然后成功运行了。这是不是说明针对80端口的监听只能是root,而其他一些非重要到端口就可以随便监听了?

冲突应该是EADDINUSE

E ACCES E ADD IN USE

Ubuntu的,没有装Apache,应该没冲突。后面使用sudo之后就可以,感觉应该是权限问题

用命令查查你的端口吧!

没有权限,我的nodeclipse插件正在纠结这个问题

首先,非root用户不能监听<1024的端口,这个是内核代码里写着的。 其次,不应该用root用户运行你的程序,这样会有安全问题。 尼玛啊,那怎么办?答案是用iptables:

sudo iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 3000
sudo iptables-save

好吧,俺也是现学现卖……

Good!

转发的话,ngnix啊,apache啊也是可以的…

只是学习没必要用80端口,就是正式使用也可以使用ngnix做转发啊

俺本来也想过来现卖一下,发现有人卖。。

可以监听80端口

普通用户只能监听1024以上的,root用户可以监听80端口

1024以下的端口都需要sudo权限运行

linux平台的非root用户要让node运行在80端口下,可以这样:

sudo setcap CAP_NET_BIND_SERVICE+ep /usr/local/bin/node

然后用普通用户试试, 注意这要linux内核在2.2后的系统,更多详细的解释可以 man capabilities.

在Node.js中,默认情况下,使用1024以下的端口号(如80端口)需要超级用户权限。这是因为这些端口被认为是“特权端口”,操作系统限制普通用户进程不能绑定到这些端口。当你尝试以普通用户身份绑定到这些端口时,会出现“EACCES”错误。

如果你希望以普通用户身份在80端口上运行你的Node.js服务器,你可以通过提升当前进程的权限来解决这个问题。在Linux或Mac系统下,你可以使用sudo命令来运行你的脚本。例如:

sudo node your-script.js

这种方式将允许你的Node.js应用程序以root权限运行,并且可以绑定到80端口。

但是请注意,运行Node.js应用为root用户存在安全风险,因此最好在实际部署时考虑其他方案,比如配置反向代理服务器(例如Nginx或Apache),让它们监听80端口并将请求转发给Node.js服务,后者可以在非特权端口上运行(例如3000)。

示例代码

如果你确实需要以root权限运行Node.js,可以使用如下示例代码,并通过命令行使用sudo执行它:

var http = require('http');

function start() {
    console.log('Call the start');
    
    function onRequest(request, response) {
        console.log('Call the onRequest function');
        response.writeHead(200, { "Content-Type": "text/plain" });
        response.write('Hello, world!');
        response.end();
    }

    http.createServer(onRequest).listen(80); // 监听80端口
    console.log('Server has started.');
}

start();

但请记住,这样做可能会带来安全隐患,应谨慎使用。

回到顶部