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

其他回答

我也有同样的问题,我只是关闭终端并打开一个新终端并运行

node server.js

一次。这对我来说是有效的,有时只需要等待几秒钟,直到它再次工作。

但这只适用于开发人员机器,而不是服务器控制台。

两个服务器不能侦听同一端口,所以检查其他服务器是否侦听同一端口,也检查浏览器同步是否运行在同一端口上

NOOB错误修复:我是Node.js的新手,并设置了一个监听端口8080的web服务器。我遇到EADDRINUSE错误。我尝试了所有不同的“杀死-9节点”迭代,并一直得到,“节点:没有发现进程”

问题是,我在调用http.listen(8080);在同一段代码中出现了两次。所以第一次它实际上工作得很好,第二次它抛出一个错误。

如果在尝试关闭端口时得到“no process found”响应,请尝试检查以确保只打开一次端口。

EADDRINUSE翻译过来就是“你试图发出app.listen()的端口正在被其他程序使用”。您可以使用这样的脚本来检查端口是否正在使用,然后在app.listen()中更改端口。

var net = require('net');
var errors = ['EADDRINUSE'];    
var isUsed = function(port) {
    var tester = net.createServer()
        .once('error', function (err) {
          if (!errors.includes(err.code)) {
           console.log("Port is in use, change the port.");
          }
        })
        .once('listening', function() {
            tester.once('close', function() { 
                console.log("You are good to go."); 
            })
            .close()
        })
        .listen(port);
}

您还可以在errors数组中添加其他错误,以检查所有类型的错误。

错误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}"...`))