从NodeJS在运行本地服务器上ECONNREFUSED [英] ECONNREFUSED on running localhost server from NodeJS

查看:164
本文介绍了从NodeJS在运行本地服务器上ECONNREFUSED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在侦听端口50000的本地计算机上启动并运行了NodeJS服务器.从另一台也在本地计算机上运行的服务器上,我需要向该服务器发出一个简单的GET请求,但我得到的只是一个ECONNREFUSED错误:

  {错误:连接ECONNREFUSED 127.0.0.1:50000在TCPConnectWrap.afterConnect上[作为oncomplete](net.js:1106:14)errno:"ECONNREFUSED",代码:"ECONNREFUSED",syscall:连接",地址:"127.0.0.1",端口:50000} 

使用 request ,我的请求如下所示:

  var选项= {网址:"http://localhost:50000",方法:"GET"}请求(选项,函数(错误,响应,正文){如果(错误){console.log("[" + getDateTime()+]连接到本地主机时出错:");console.log(错误);返回;}//继续 ... 

我知道服务器已启动并且正在运行,并且已经定义了端点,因为我可以在邮递员或浏览器中向完全相同的url发出请求,并获得响应,但是在我的NodeJS代码中却没有./p>

有人有主意吗?

解决方案

可能的问题是,其他进程已在您要使用的同一端口上运行,请更改端口或杀死该端口上的现有进程.要终止端口上的进程,您可以尝试:

对于Mac:

  sudo kill $(lsof -t -i:8000)# 或者须藤热熔器-k -n tcp 8000# 或者热熔器-k 8000/tcp 

对于Windows,请检查

希望这会有所帮助:)

I have a NodeJS server up and running on my local machine that is listening to port 50000. From another server, that is also running on my local machine, I need to make a simple GET request to that server, but all I get is an ECONNREFUSED error:

{ Error: connect ECONNREFUSED 127.0.0.1:50000
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1106:14)
  errno: 'ECONNREFUSED',
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '127.0.0.1',
  port: 50000 }

My request looks as follows, using request:

var options = {
    url: "http://localhost:50000",
    method: "GET"
}
request(options, function(error, response, body) {
    if (error) {
        console.log("[" + getDateTime() + "] Error connecting to localhost:");
        console.log(error);
        return;
   }
   // continue ...

I know that the server is up and running and that the endpoint is defined, because I can do the request to that exact same url in postman or in my browser and get a response, but somehow not in my NodeJS code.

Anybody have an idea?

解决方案

The possible issue is that some else process is already running on the same port you are trying to use, either change your port or kill the existing process on your port. To kill the process on port you can try:

For mac:

sudo kill $(lsof -t -i:8000) 
# or 
sudo fuser -k -n tcp 8000 
# or 
fuser -k 8000/tcp

And for windows check this

hope this helps :)

这篇关于从NodeJS在运行本地服务器上ECONNREFUSED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆