Skip to content Skip to sidebar Skip to footer

How To Display Image From Http Request To External Api With Node.js

I have a situation where in order to get images for a site that I am building, I need to make a http request to an external server for information. Currently, the responses from th

Solution 1:

Step 1: Fetch image and save it on node server. request module documentation on streaming for more options

request('http://google.com/doodle.png').pipe(fs.createWriteStream('doodle.png'));

Step 2: send the saved image as response.

app.get('/display', function(req, res)) {
  fs.readFile('doodle.png', function(err, data) {
    if (err) throw err; // Fail if the file can't be read.else {
      res.writeHead(200, {'Content-Type': 'image/jpeg'});
      res.end(data); // Send the file data to the browser.
    }
  });
};

Post a Comment for "How To Display Image From Http Request To External Api With Node.js"