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

当前回答

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

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

其他回答

当杀死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

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

更改端口就可以了

node-inspector --web-port=8099

在下面的命令替换您的端口编号

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

EADDRINUSE表示你的nodejs应用的端口已经被使用。

现在你已经杀死了在该端口上运行的进程/应用程序。 通过以下方式查找app的进程id:

-i tcp:3000

现在你会从这里得到进程id。 运行这个:

kill -9 进程 ID

查看哪些进程正在使用该端口也很有用。

对于macOS,我从这篇文章中发现,AirPlay使用端口5000,所以即使你使用这里列出的解决方案杀死它,它也会再次使用端口5000。快速搜索谷歌,查看哪些常见应用程序或进程正在使用导致错误的端口。