Read remote image and return in request

I have some images on a CDN.

However for security reasons I need some images to be loaded from the same subdomain as the page.

module.exports = function relayImage(req, res) {
  https.get("https://cdn.mysite.com/foo.jpg", (resp) => {
    const dataBlocks = []
    resp.on('data', data => dataBlocks.push(data));
    resp.on('end', err => {
       if(err){
         console.error(err)
         res.end()
         return
       }
       res.set({
         'accept-ranges': "bytes",
         'cache-control': resp.headers['cache-control'],
         'content-type': resp.headers['content-type'],
         'content-length': resp.headers['content-length'],
       });
       dataBlocks.forEach(data => res.send(data))
    })
  })
}

For some reason this only returns the first 2% of the image top



from Recent Questions - Stack Overflow https://ift.tt/377o5Oo
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation