nodejspipe道https响应request.post和写入文件

我正在做HTTP代理程序,检查HTTP URL,如果它是下载链接(内容types:八位字节stream),我会得到响应,并通过使用request.post和其他计算机下载文件与其他计算机响应中继该响应由http代理给出。

假设networking代理计算机是A.并且它是A. 192.168.5.253的代码的一部分

if(contentType && (contentType== "application/octet-stream" || contentType == "application/gzip")){ console.log("remoteRes##app",remoteRes); let filepath = req.url.split('/'); let FileName = getFilename(remoteRes, filepath); let writeStream = fs.createWriteStream(FileName); /*remoteRes is octect-stream response. I can get file buffer If I use remoteRes.on(data, chunk => {...})*/ remoteRes.pipe(writeStream); //It works but I want to send file buffer to B without writing file. ......... 

我可以在A下载文件,但我想发送这个响应到PC B(192.168.5.32:10001)服务器。 所以我想要这样stream:

 remoteRes.pipe(request.post('http://192.168.5.32:10001/upload)); 

这是服务器B(192.168.5.32)代码的一部分

 router.post('/upload', (req, res, next) => { let wstream = fs.createWriteStream('ffff.txt'); req.pipe(wstream); //It dosen't work, but I want to do like this. }) 

我想在router.post('/ upload')中获得filebuffer。 如果这是后置或放置,这不重要。 我看到,当我使用remoteRes.pipe(request.post(' http://192.168.5.32:10001/upload )); ,我看到来自ServerA的请求到达ServerB。 但是我无法在ServerB中获得文件缓冲区。 总之,我想piperequest.post响应。

您需要使用自己的中间件来存储传入的缓冲区,因此它将在路由器请求处理程序中可用


在这里你有一个工作的例子(你可以保存它并testing它作为一个单一的文件):

 //[SERVER B] const express = require('express'); const app = express() //:Middleware for the incoming stream app.use(function(req, res, next) { console.log("[request middleware] (buffer storing)") req.rawBody = '' req.on('data', function(chunk) { req.rawBody += chunk console.log(chunk) // here you got the incoming buffers }) req.on('end', function(){next()}) }); //:Final stream handling inside the request app.post('/*', function (req, res) { /* here you got the complete stream */ console.log("[request.rawBody]\n",req.rawBody) }); app.listen(3000) //[SERVER A] const request = require('request') request('http://google.com/doodle.png').pipe(request.post('http://localhost:3000/')) 

我希望你能推断出你的具体用例。