I am writing a simple Express API integrating with Google Places API, and trying to send the place photo down to the client, but can't get it to work. The response from the Google Places API looks something like this:
The response object also contains a headers
property. I've tried sending the image back like this:
router.get('/photo/:photoRef', function (req, res) {
var params = {
maxwidth: 400,
photoreference: req.params.photoRef,
key: key
};
var url = baseUrl + 'photo?' + querystring.stringify(params);
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
res.type(response.headers['content-type']);
res.send(response.body);
}
});
});
but that doesn't seem to work. I get the following image:
Any help would be greatly appreciated.
Express is a node js web application framework that provides broad features for building web and mobile applications. It is used to build a single page, multipage, and hybrid web application. It's a layer built on the top of the Node js that helps manage servers and routes.
Set the response type to image/png.
res.set({'Content-Type': 'image/png'});
To save this data as an image in your client app refer here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With