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

当前回答

在控制器环境下,你可以使用:

在运行脚本之前,Pkill节点应该完成这项工作。

请记住,这个命令会杀死所有的节点进程,如果你有一个容器只运行一个实例,这可能是正确的,如果你有这样的环境,你可以保证。

在任何其他场景中,我建议使用命令终止通过编程方式查找的某个进程id或名称。比如,如果你的进程被调用node-server-1你可以执行pkill node-server-1。

这个资源可能有助于理解: https://www.thegeekstuff.com/2009/12/4-ways-to-kill-a-process-kill-killall-pkill-xkill/

其他回答

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

我最近也遇到了同样的问题。

这意味着该端口已经被另一个应用程序(express或其他软件)使用。

在我的情况下,我不小心在2个终端上运行express,所以使用“Ctrl + C”退出终端为我解决了问题。(只从一个终端运行服务器)

希望它能帮助别人。

Windows在开源方面总是很棘手。

更改端口就可以了

node-inspector --web-port=8099

当杀死NODE_PORT时,它可能会杀死你的chrome进程或任何侦听相同端口的东西,这很烦人。

这个shell脚本可能会有帮助——在我的例子中,端口是1337,但您可以随时更改它

# LOGIC

CHROME_PIDS=`pidof chrome`
PORT_PIDS=`lsof -t -i tcp:1337`

for pid in $PORT_PIDS
do

if [[ ${CHROME_PIDS} != *$pid* ]];then

    # NOT FOUND IN CHROME PIDS

    echo "Killing $pid..."
    ps -p "$pid"

    kill -kill "$pid"
    fi

done

sails lift
# OR 'node app' OR whatever that starts your node

exit

您的应用程序已经在端口8080上运行。 使用此代码杀死端口并再次运行您的代码

sudo lsof -t -i tcp:8080 | xargs kill -9