如果我运行一个端口80的服务器,我尝试使用XMLHttpRequest,我得到这个错误

为什么NodeJS会有问题,如果我想做一个请求,而我在端口80上运行一个服务器?对于网络浏览器来说,这不是问题:我可以在服务器运行时上网。

服务器为:

  net.createServer(function (socket) {
    socket.name = socket.remoteAddress + ":" + socket.remotePort;
    console.log('connection request from: ' + socket.remoteAddress);
    socket.destroy();
  }).listen(options.port);

请求是:

var xhr = new XMLHttpRequest();

xhr.onreadystatechange = function() {
    sys.puts("State: " + this.readyState);

    if (this.readyState == 4) {
        sys.puts("Complete.\nBody length: " + this.responseText.length);
        sys.puts("Body:\n" + this.responseText);
    }
};

xhr.open("GET", "http://mywebsite.com");
xhr.send();

当前回答

这适用于我(我使用mac)。执行此命令

lsof -PiTCP -sTCP:LISTEN

这将显示系统正在使用的端口列表。找到您的节点正在运行的PID

COMMAND     PID          USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
node      17269 hientrq   16u  IPv6 0xc42959c6fa30c3b9      0t0  TCP *:51524 (LISTEN)
node      17269 hientrq   19u  IPv4 0xc42959c71ae86fc1      0t0  TCP localhost:1337 (LISTEN)

运行kill -9 [YOUR_PID]

其他回答

尝试这两个命令,它将停止所有节点进程。

killall 9 node
pkill node
npm start 

如果你想解决这个问题

$ node server events.js:141 throw er; // Unhandled 'error' event ^ Error: listen EADDRINUSE :::3000 at Object.exports._errnoException (util.js:907:11) at exports._exceptionWithHostPort (util.js:930:20) at Server._listen2 (net.js:1250:14) at listen (net.js:1286:10) at Server.listen (net.js:1382:5) at EventEmitter.listen (C:\sendbox\mean\node_modules\express\lib\application .js:617:24) at Object. (C:\sendbox\mean\server.js:28:5) at Module._compile (module.js:409:26) at Object.Module._extensions..js (module.js:416:10) at Module.load (module.js:343:32)

将端口号更改为8000

在Debian上,我发现要在端口80上运行,你需要以根用户的身份发出命令

sudo node app.js

我希望这对你们有帮助

另一个可能产生此错误的是相同节点代码中的两个HTTP服务器。我正在更新一些Express 2到Express 3的代码,然后有了这个…

http.createServer(app).listen(app.get('port'), function(){            
  console.log('Express server listening on port ' + app.get('port')); 
});        

// tons of shit.

http.createServer(app).listen(app.get('port'), function(){            
  console.log('Express server listening on port ' + app.get('port')); 
});                                                                   

它触发了这个错误。

似乎有另一个Node ng服务进程正在运行。在你的控制台(Linux/Mac)中输入以下内容进行检查:

ps aux|grep node

然后用:

kill -9 <NodeProcessId>

或替代使用

ng serve --port <AnotherFreePortNumber>

在你选择的自由港口为你的项目服务。