Express-从远程Web服务返回二进制数据 [英] Express - Return binary data from distant webservice
问题描述
我尝试使用Express返回一些二进制数据.在示例中,它是PDF,但从理论上讲,它可以是任何类型的文件.
I try to return some binary data with Express. In the example, it's a PDF but theorically, this can be any sort of file.
但是暂时将重点放在pdf上.我写了这段代码:
But focus on the pdf for the moment. I wrote this code :
app.get('*', function (req, res) {
getBinaryData(req.url,
function (answer) {
res.type('pdf');
res.end(new Buffer(answer, 'binary'));
},
function (error) {
res.setHeader('Content-Type', 'text/plain');
return res.end(error);
}
);
});
基于我在这里看到的内容: https://github.com/strongloop/express/issues /1555
Based on what I saw here : https://github.com/strongloop/express/issues/1555
但是,我得到的PDF文件具有正确的页数,正确的标题....但是所有页面都是空白
But, i get a pdf file with the right number of pages, right title.... but all the pages are blank
我肯定会担心getBinaryData()的返回,因为此函数询问了外部Web服务,当我直接询问该服务时,我得到了正确的文档.
I'm sure concern the return of getBinaryData(), because this function asked an external Web Service and when I asked directly this service, I got the right document.
预先感谢您的回答
推荐答案
我找到了一个更简单的解决方案:
I found a more simple solution :
request(req.url).pipe(res);
这会将来自远程Web服务的原始响应直接传递给我的响应!无论文件类型如何,我都能得到正确的文件.
This pipes the original response from distant Web Service directly to my response! I got the correct file regardless of the file type.
这篇关于Express-从远程Web服务返回二进制数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!