How To Request Images And Output Image In Node.js
I try to get the image and display on a url. and I use request module. For example, I want to get the image https://www.google.com/images/srpr/logo11w.png, and display on my url ht
Solution 1:
Try specifying encoding: null
when making the request so that the response body is a Buffer
that you can directly write to the response stream:
app.get("/google/logo", function(req, res) {
var requestSettings = {
url: 'https://www.google.com/images/srpr/logo11w.png',
method: 'GET',
encoding: null
};
request(requestSettings, function(error, response, body) {
res.set('Content-Type', 'image/png');
res.send(body);
});
});
On the other hand if you do not specify encoding: null
, the body
parameter will be a String instead of a Buffer.
Solution 2:
That seems an overkill, you can just ask the browser the get the url directly like this;
app.get("/google/logo", function(req, res) {
res.writeHead(302, {location:"https://www.google.com/images/srpr/logo11w.png"});
res.end();
})
Post a Comment for "How To Request Images And Output Image In Node.js"