[Fixed] Piping remote file in ExpressJS

Issue

I would like to read a remote image and display it. I can save the file but not getting the code right to display it. Ideally I just want to pass the file right though without processing – not sure if a tmp file step is required or not. This code displays nothing – no errors. I tried res.pipe(response) as well.

var url = 'http://proxy.boxresizer.com/convert?resize=50x50&source=' + filename

var request = http.get(url, function(response) {

  var tmp = path.join(require('os').tmpDir(), filename);

  var outstream = require('fs').createWriteStream(tmp);

  response.pipe(outstream);
  response.on('end', function() {
    res.set('Content-Type', 'image/jpg');
      res.pipe(outstream);
      res.end();
  });
});

Solution

Well I’d still like to know how to make the above work but I solved my issue in one line with the request module!

var url = 'http://proxy.boxresizer.com/convert?resize=50x50&source=' + filename
require('request').get(url).pipe(res);  // res being Express response

Leave a Reply

(*) Required, Your email will not be published