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

当前回答

错误:listen EADDRINUSE表示要分配/绑定到应用服务器的端口已经在使用中。您可以为您的应用程序分配另一个端口。

或者如果你想给应用程序分配相同的端口。然后杀死在你想要的端口上运行的应用程序。

对于一个节点应用程序,你可以尝试的是,找到节点应用程序的进程id:

ps -aux | grep node

在获得进程id之后,执行

kill process_id

其他回答

真正帮助我的是:

killall -9 node

但是这会杀死一个系统进程。

ps ax

你可以检查它是否有效。

错误EADDRINUSE(地址已在使用)报告本地系统上已经有另一个进程占用该地址/端口。

有一个叫做find-process的npm包可以帮助查找(并关闭)占用进程。

下面是一个小演示代码:

const find = require('find-process')

const PORT = 80

find('port', PORT)
.then((list) => {
  console.log(`Port "${PORT}" is blocked. Killing blocking applications...`)
  const processIds = list.map((item) => item.pid)
  processIds.forEach((pid) => process.kill(pid, 10))
})

我准备了一个小样本,可以重现EADDRINUSE错误。如果你在两个不同的终端上启动下面的程序,你会看到第一个终端将启动一个服务器(在端口“3000”上),而第二个终端将关闭已经运行的服务器(因为它阻止了第二个终端EADDRINUSE的执行):

最小工作示例:

const find = require('find-process')
const http = require('http')

const PORT = 3000

// Handling exceptions
process.on('uncaughtException', (error) => {
  if (error.code === 'EADDRINUSE') {
    find('port', PORT)
      .then((list) => {
        const blockingApplication = list[0]
        if (blockingApplication) {
          console.log(`Port "${PORT}" is blocked by "${blockingApplication.name}".`)
          console.log('Shutting down blocking application...')
          process.kill(blockingApplication.pid)
          // TODO: Restart server
        }
      })
  }
})

// Starting server
const server = http.createServer((request, response) => {
  response.writeHead(200, {'Content-Type': 'text/plain'})
  response.write('Hello World!')
  response.end()
})

server.listen(PORT, () => console.log(`Server running on port "${PORT}"...`))

这适用于我(我使用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]

错误:listen EADDRINUSE表示要分配/绑定到应用服务器的端口已经在使用中。您可以为您的应用程序分配另一个端口。

或者如果你想给应用程序分配相同的端口。然后杀死在你想要的端口上运行的应用程序。

对于一个节点应用程序,你可以尝试的是,找到节点应用程序的进程id:

ps -aux | grep node

在获得进程id之后,执行

kill process_id

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

它触发了这个错误。