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

168
Visualizações
Changing the header of the response on NodeJS using request

I have the following problem:

I want to get a static file from another server, and give it back to the user with another content-type header.

The following code works just fine, but I can't figure out a way to change the response header, though.

const request = require('request');

app.get('video', function (req, res) {
    request.get('http://anotherurl.com/video-sample.mp4').pipe(res);
});

I tried to do this thing more manually, but the response was very slow.

app.get('video', function (req, res) {
  request.get('http://anotherurl.com/video-sample.mp4', function(error, response, body) {
    // ...
    res.setHeader('content-type', 'image/png');
    res.send(new Buffer(body));
  });
});

Can you guys help me with that?

Thanks

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

0

Just set the response header when the 'response' event fires.

app.get('video', (req, res) => {
    request.get('http://anotherurl.com/video-sample.mp4')
    .on('response', response => {
      res.setHeader('Content-Type', 'image/png');

      // pipe response to res 
      // since response is an http.IncomingMessage
      response.pipe(res);
    });
});
about 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