如何使用Express / Node.js将图像发送到客户端?
我正在编写一个简单的Express API与Google Places API集成,并试图将地点照片发送到客户端,但无法使其工作。 Google Places API的回复如下所示:
响应对象还包含一个headers
属性。 我试过像这样发回图像:
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); } }); });
但似乎并不奏效。 我得到以下图像:
任何帮助将不胜感激。
将响应types设置为image / png。
res.set({'Content-Type': 'image/png'});
要将这些数据保存为客户端应用程序中的图片,请参阅此处