Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!