Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

Express 和 request 如何代理远程图片?

使用 Node 的 Express 结合 request 来代理远程图片,但是返回的内容和原图片的内容有区别,是乱码,但是乱的不一致。
关键代码:

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);
};

原图片:

clipboard.png

代理后返回的图片:

clipboard.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

加了 encoding: null 就可以了

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...