简体   繁体   中英

Sending image file from http request in Node.js

I am trying to setup an API that will return an image from my google places API using google's photo reference ID as a parameter. This is what I have so far:

module.exports.getPhoto=function(req,res){
    var id=req.params.id;

    var url='https://maps.googleapis.com/maps/api/place/photo?maxwidth=400&photoreference='+id+'&key='+process.env.GOOGLEAPI;

    request.get(url, function (err,response,body) {  
      if(err){
        res.status(400).json(err);
      }else{
         res.send(body);
      }    
    });
};

Right now the body is not sending in the correct format. Is there a way to do this with out saving it as a file and then sending it?

Try setting correct headers for serving image itself:

res.set('Content-Type', 'image/gif');

before you send a request

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM