[英]Images are not proxied correctly when using NodeJS + Request module
我有一個運行良好的代理(據我所知),直到我嘗試代理圖像(或任何二進制數據?)。 據我估計,下面的代碼應該可以,但是不能。 我敢肯定我在做一些顯然很愚蠢的事情,但是我一直在研究論壇和api,但尚未找到正確的方法。
我的代理服務器的“核心”如下所示。
function(req, res) {
...
options = {
url: 'a url',
headers: {
'Authorization': auth
}
};
request(options,
function(e, r, b){
var encoding = (r.headers['content-type'].indexOf('image') === -1) ? 'utf8' : 'binary';
res.writeHead(200, {
'Content-Length': r.headers['content-length'],
'Content-Type': r.headers['content-type']
});
if (encoding === 'binary') {
b = new Buffer(b);
}
res.end(b, encoding);
});
}
我在這里想念什么?
在此先感謝您提供的所有幫助!
我的問題不是響應(就像我最初那樣),而是請求模塊默認將其響應主體編碼為unicode的事實,當禁用時( encoding: null
),響應主體將轉換為緩沖區容易被響應消耗掉。
options = {
url: url,
encoding: null,
headers: {
'Authorization': auth
}
};
request(options,
function(e, r, b){
var encoding = (r.headers['content-type'].indexOf('image') === -1) ? 'utf8' : 'binary';
res.end(b, encoding);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.