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

440
Visualizações
How to forward a node request using axios?

In my express.js server I am doing this to forward a request to another server

async handleRequest(req, res) {
    const serverUrl = "http://localhost:4000"

    await req.pipe(request({ url: serverUrl + req.url })).pipe(res);
}

This is working as expected. But I have decided to use axios or got library instead of request since request is no longer maintained. But something like this is not working -

req.pipe(axios({url: serverUrl + req.url})).pipe(res);

I am getting error

(node:88124) UnhandledPromiseRejectionWarning: TypeError: dest.on is not a function
    at IncomingMessage.Readable.pipe (internal/streams/readable.js:671:8)

How can I fix this? I want to forward the request as it is without making any changes in the request object.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You need to use responseType parameter with value stream so axios give you the option to pipe your response data as a stream by:

axiosResponse.data.pipe(destination); // where destination can be a file or another stream

Also, in fact there is no need to do await req.pipe because axios will stream the response into your express response (res) object

hence is the full answer:

  const axiosResponse = await axios({
    url: serverUrl + req.url,
    responseType: 'stream'
  })

  axiosResponse.data.pipe(res)
over 4 years ago · Santiago Trujillo 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