nodejs pipe https对request.post和写入文件的响应
问题描述:
我使http代理程序检查http url,如果它是下载链接(content-type:octet-stream),我会得到响应并将响应中继给其他计算机通过使用request.post和其他计算机下载文件与http代理给出的响应。nodejs pipe https对request.post和写入文件的响应
让我们假设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.
.........
我可以下载A.文件,但我要发送此响应PC B( 192.168.5.32:10001)服务器。 所以我想流是这样的:
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 filebuffer( '/上传' )。它不管是后置还是放。 我看到,当我使用remoteRes.pipe(request.post('http://192.168.5.32:10001/upload)); ,我看到来自ServerA的请求到达ServerB。但是我无法在ServerB中获取文件缓冲区。 总之,我想管道响应request.post。
答
您需要使用您自己的中间件来存储进入缓冲区,所以它会在路由器请求处理
这里有一个工作示例(你可以将它保存并测试它作为一个单一的可文件):
//[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/'))
我希望你能推断这个为你的具体用例。
1)** A **获取文件并将其发布到** B ** ** B **将其写入文件并回复到源自** A **的帖子,这是场景? –
EMX
通过request.post向B写入文件缓冲区的管道响应,或者将B写入B文件。我不需要回答。 – jijijijiji