How do Express and request proxy remote images?
習慣沉默
習慣沉默 2017-05-16 13:45:34
0
3
560

Use Node's Express combined with request to proxy remote images, but the content returned is different from the content of the original image. It is garbled, but the chaos is inconsistent.
Key code:

var FurionImgHandler = function (req, res) { var url = req.url.split('/fimg/')[1]; var options = { url: url }; function callback (error, response, body) { if (!error && response.statusCode === 200) { var contentType = response.headers['content-type']; response.setEncoding('binary'); res.set('Content-Type', contentType); res.send(body); } } request.get(options, callback); };

Original picture:

Pictures returned after proxying:

習慣沉默
習慣沉默

reply all (3)
習慣沉默

Just addencoding: nulland that’s it

var options = { url: url, encoding: null };
    阿神

    It should be the passed Blob object. Try converting it.

      大家讲道理

      If the image does not require storage or other operations, can't it be passed directly to the response through the pipe?

      http.get(options, (response) => { response.pipe(res); })

      soonfy

        Latest Downloads
        More>
        Web Effects
        Website Source Code
        Website Materials
        Front End Template
        About us Disclaimer Sitemap
        php.cn:Public welfare online PHP training,Help PHP learners grow quickly!