2017-08-01 155 views
1

我有一个API服务器和的NodeJS服务器,当被请求文件的NodeJS重定向请求API服务器发送文件从AP服务器的NodeJS到浏览器

API服务器发送文件作为原始数据的NodeJS 和Nodejs将文件重定向到浏览器

但是,当我使用Wireshark检查网络数据时,浏览器接收到的数据包不是原始数据,因为它来自API服务器(适用于文本文件,但不适用于图像,视频,pdf ,doc等)

router.get('/GetCaseSupportDocument', function (req, res) { 

    var MyJsonData = { 
     DocId:parseInt(req.query.DocId) || 0 
    }; 

    request({ 
     url: 'http://somedomain/someurl', //URL to hit 
     method: 'POST', 
     json: MyJsonData 

    }, function (error, response, body) { 
     if (error) { 
       res.status(200).send('Failed'); 
     } else { 
       res.status(200).send(body); 
      } 
    }) 

}); 

任何人都可以告诉它为什么它会在NodeJ之间更改浏览器? 这种传输方式有没有更好的解决方案?

更新后找到解决方案。这工作

router.get('/GetCaseSupportDocument', function (req, res) { 

     var MyJsonData = { 
      DocId:parseInt(req.query.DocId) || 0 

     }; 

     request({ 
      url: Url.CaseService + 'GetCaseSupportDocument', //URL to hit 
      method: 'POST', 
      json: MyJsonData 
     }).pipe(res); 

}) 
+0

只是次要的评论:如果遇到错误,您可能不应返回200的状态。如果您可以更清楚地分辨错误类型,则可以确切地确定它应该是什么,但我同时还会返回500。 – HomerPlata

+0

@HomerPlata。我只是想在外部世界隐藏应用程序中API服务器的存在.. Dats y返回“失败”。无论如何感谢... 就像我说我得到的回应。但它在一些字符开始后改变了某些字符,并在文本文件的情况下运行良好 – DrVishnu

回答

1

有一个简单的代理使用流,你可以尝试:

router.get('/GetCaseSupportDocument', function (req, res) { 
    var MyJsonData = { 
    DocId: parseInt(req.query.DocId) || 0 
    }; 

// updated the response 
request({ 
    url: 'http://somedomain/someurl', //URL to hit 
    method: 'POST', 
    json: MyJsonData 
    }).pipe(res); 

}); 

更多细节与代理-ING,你可以找到request文档https://github.com/request/request

+1

试图以这种方式bt我得到这个错误---- stream.js:74 throw er; //管道中未处理的流错误。 ^ 错误:结束 后写在ClientRequest.OutgoingMessage.write(_http_outgoing.js:439:15) – DrVishnu

+1

req.pipe在开始时没有needed..nw它的工作原理...编辑我的问题与答案 – DrVishnu

+0

@DrVishnu更新对耐心的回应thx –

相关问题