Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Piping remote file in ExpressJS

Tags:

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();   }); }); 
like image 630
cyberwombat Avatar asked Aug 25 '13 19:08

cyberwombat


People also ask

Is ExpressJS outdated?

It is unmaintainedExpress has not been updated for years, and its next version has been in alpha for 6 years. People may think it is not updated because the API is stable and does not need change.

Is ExpressJS good for production?

js framework for web development. It's fast, unopinionated, and has a large community behind it. It is easy to learn and also has a lot of modules and middleware available for use. Express is used by big names like Accenture, IBM, and Uber, which means it's also great in a production environment.

Is ExpressJS and NodeJS same?

What is Express JS – Quick Overview. ExpressJS is a web application framework for NodeJS. That's what mainly makes the difference between Express JS and Node JS. The former provides various features that make web application development fast and easy, which otherwise takes more time using only the latter.

Is ExpressJS a backend?

Is ExpressJS frontend or backend? Express. JS is an open-source and free-to-use backend framework to develop web applications.


2 Answers

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 
like image 107
cyberwombat Avatar answered Oct 23 '22 07:10

cyberwombat


Since request is deprecated, you can alternatively use node-fetch like so:

app.get("/", (req, res) => {     fetch(actualUrl).then(actual => {         actual.headers.forEach((v, n) => res.setHeader(n, v));         actual.body.pipe(res);     }); }); 
like image 20
ABabin Avatar answered Oct 23 '22 05:10

ABabin