Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

159
Visualizações
Error: connect ECONNREFUSED 127.0.0.1 when connection to anything on localhost

I try to connect from a node.js backend to a different server running on localhost. Every server is running in a docker container. When I execute the http request I receive 'Error: connect ECONNREFUSED 127.0.0.1:80' or 'Error: connect ECONNREFUSED Error: connect ECONNREFUSED 127.0.0.1:4200' when connection to anything on localhost. My code looks the following:

const http = require('http');

const xy = http.get('http://localhost/path', (r) =>{
  console.log(r);
});
xy.on('error', e => {
  console.log(e);
});

const xz = http.get('http://localhost:4200', (r) =>{
  console.log(r);
});
xz.on('error', e => {
  console.log(e);
});

I do not understand, why it does not work. The same request in the browser does work. Connecting to different URLs like google.com does also work. Here is the output of netstat -na:

tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN    
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:5432            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:4222            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:4200            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:9000            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN  

Does anyone have an idea?

Regards

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

localhost inside a container refers to the container itself, not your laptop/server. So the other service is not running there.

If you want to connect from a container to another container you should use the name of the container instead localhost.

Also, containers need to be inside the same network

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda