简体   繁体   English

nodejs将https响应传递给request.post并写入文件

[英]nodejs pipe https response to request.post and write file

I am Making http proxy program that check http url and If It is download link(content-type:octet-stream), I would get response and relay that response to other computer by using request.post and other computer download file with response that are given by http proxy. 我正在制作检查http url的http代理程序,如果它是下载链接(content-type:octet-stream),我将获得响应并将该响应通过使用request.post和其他具有响应结果的计算机下载文件转发给其他计算机。由http代理提供。

Let's suppose that web proxy computer is A. And It is part of code of A. 192.168.5.253 假设Web代理计算机是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.
 .........

I can download file in A. but I want to send this response to pc B(192.168.5.32:10001) server. 我可以在A中下载文件,但我想将此响应发送到pc B(192.168.5.32:10001)服务器。 So I want to streaming like this: 所以我想像这样流式传输:

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

And this is part of Server B(192.168.5.32) code 这是服务器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. 

})

I want to get filebuffer in router.post('/upload'). 我想在router.post('/ upload')中获取文件缓冲区。 It dosen't matter If It is post or put. 张贴或放置都没关系。 I saw that when I use remoteRes.pipe(request.post(' http://192.168.5.32:10001/upload )); 我看到当我使用remoteRes.pipe(request.post(' http://192.168.5.32:10001/upload )); ,I saw that request from ServerA get to ServerB. ,我看到来自ServerA的请求到达了ServerB。 But I couldn't get file buffer in ServerB. 但是我无法在ServerB中获得文件缓冲区。 In short, I want to pipe response to request.post. 简而言之,我想将响应传递给request.post。

You need to store the incoming buffers using your own middleware, so it will be available in the routers request handler 您需要使用自己的中间件存储传入的缓冲区,因此它将在路由器请求处理程序中可用


Here you have a working example (you can save it and test it as a single file) : 在这里,您有一个有效的示例(可以将其保存并作为一个文件进行测试):

//[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/'))

I hope you can extrapolate this for your specific use case. 我希望您可以针对您的特定用例进行推断。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM