Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to proxy an image in node-js/express

I get a broken image link when I try to access this via Express:

app.get('/fileThumbnail', function(req, res) {

var url = proxiedURL +"?" + querystring.stringify(req.query);

logger.info('/fileThumbnail going to url', url);

request.get(url, function(err, response, img) {
    logger.info("response:", response.statusCode, response.headers['content-type']);
    if (!err && response.statusCode === 200) {
        res.writeHead(200, {
            'Content-Type': response.headers['content-type']
        });
        // response.pipe(res);  // not working
        res.writeHead(200, response.headers);
        res.end(img, 'binary');
    } else res.send("Error occurred:", err, "; status code: ", response.statusCode);
})
});

The thumbnail is coming from a PHP server:

if (!$thumbnail) {
    $thumbnail = $_SERVER['DOCUMENT_ROOT'].'/images/spacer.gif';
}


// Write the thumbnail
header('Content-Type: '.image_type_to_mime_type(exif_imagetype($thumbnail)));
header('Content-length: '.filesize($thumbnail));
print file_get_contents($thumbnail,FILE_BINARY);

Have tried several things; response headers are as follows:

Access-Control-Allow-Methods:GET,POST,PUT,DELETE,OPTIONS
access-control-allow-origin:*
cache-control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
connection:Keep-Alive
content-length:894
content-type:image/jpeg
date:Mon, 17 Nov 2014 22:13:24 GMT
expires:Thu, 19 Nov 1981 08:52:00 GMT
keep-alive:timeout=5, max=100
pragma:no-cache
server:Apache/2.2.15
X-Powered-By:Express
like image 772
Jeff Lowery Avatar asked Nov 17 '14 22:11

Jeff Lowery


2 Answers

As usual, the simple solution is the best. Simply npm install request and then use it like this:

var request = require('request');

app.get('/fileThumbnail', function(req, res) {
    var url = proxiedURL +"?" + querystring.stringify(req.query);
    logger.info('/fileThumbnail going to url', url); 
    request.get(url).pipe(res);
});
like image 110
Jeff Lowery Avatar answered Nov 12 '22 04:11

Jeff Lowery


Using pipe() didn't work for my version of Express, however this implemention did. It proxies http and https image urls.

Call with:

http://example.com/image?url=https%3A%2F%2Fwww.google.com%2Fimages%2Fbranding%2Fgooglelogo%2F2x%2Fgooglelogo_color_272x92dp.png

Source:

var url         = require('url');
var http        = require('http');
var https       = require('https');

app.get('/image', function (req, res) {
    var parts = url.parse(req.url, true);
    var imageUrl = parts.query.url;

    parts = url.parse(imageUrl);

    var filename = parts.pathname.split("/").pop();

    var options = {
        port: (parts.protocol === "https:" ? 443 : 80),
        host: parts.hostname,
        method: 'GET',
        path: parts.path,
        accept: '*/*'
    };

    var request = (options.port === 443 ? https.request(options) : http.request(options));

    request.addListener('response', function (proxyResponse) {
        var offset = 0;
        var contentLength = parseInt(proxyResponse.headers["content-length"], 10);
        var body = new Buffer(contentLength);

        proxyResponse.setEncoding('binary');
        proxyResponse.addListener('data', function(chunk) {
            body.write(chunk, offset, "binary");
            offset += chunk.length;
        }); 

        proxyResponse.addListener('end', function() {
            res.contentType(filename);
            res.write(body);
            res.end();            
        });
    });

    request.end();
});
like image 3
David Boyd Avatar answered Nov 12 '22 02:11

David Boyd