如果我运行一个端口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();

当前回答

这个选项对我来说是有效的:

Run:

ps -ax | grep node

你会得到这样的东西:

 8078 pts/7    Tl     0:01 node server.js
 8489 pts/10   S+     0:00 grep --color=auto node    
 kill -9 8078

其他回答

EADDRINUSE表示端口(我们在节点应用程序中尝试侦听的端口)已经被使用。为了克服这个问题,我们需要确定哪个进程正在使用该端口运行。

例如,如果我们试图在3000端口监听节点应用程序。我们需要检查该端口是否已经被任何其他进程使用。

步骤1:

$sudo netstat -plunt |grep :3000

上面的命令给出下面的结果。

tcp6       0      0 :::3000                 :::*                    LISTEN      25315/node

步骤2:

现在您获得了进程ID(25315),杀死该进程。

kill -9 25315

步骤3:

npm run start

注意:此解决方案适用于linux用户。

我更喜欢做

Killall -15节点

因为kill -15给了进程一个自我清理的机会。 现在,你可以通过

Ps aux | grep节点

注意:如果您不给进程一个机会来完成它当前正在做的事情并进行清理,可能会导致文件损坏

另一个可能产生此错误的是相同节点代码中的两个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')); 
});                                                                   

它触发了这个错误。

我以前(在节点中)用http看到过这个错误。我记得,这个问题与没有初始化httpClient或在httpClient创建和/或url请求中设置错误的选项有关。

对于windows 10上的其他人来说,node是localhost,端口是3500,而不是80…

不奏效的:

killall    ?  command not found
ps -aux | grep 'node'     ?     ps:  user x unknown 

显示信息但仍然不起作用的:

 ps -aef | grep 'node'
 ps ax
 kill -9 61864

哪些方法有效:

Git Bash或Windows上的Powershell

  net -a -o | grep 3500   (whatever port you are looking for) 

注意PID(最右边) 我不能让所有人都去工作……所以

打开任务管理器 在进程选项卡上,右键单击名称或任何列,并选择包含PID 按PID排序,然后右键单击右PID,然后单击结束任务。

现在,在windows上不那么有趣的练习之后,我意识到我可以使用任务管理器,找到Node引擎,然后结束它。

仅供参考,我使用Visual Studio代码在端口3500上运行节点,我在VS代码中使用Git Bash shell。我已经用Ctrl + C优雅地退出了,但有时这并不能杀死它。我不想改变我的端口或重新启动,所以这是有效的。希望它能帮助到其他人。否则就是我自己的文档。