[英]NodeJS how to debug the difference between a https.request and request.post output?
[英]nodejs pipe https response to request.post and write file
我正在制作检查http url的http代理程序,如果它是下载链接(content-type:octet-stream),我将获得响应并将该响应通过使用request.post和其他具有响应结果的计算机下载文件转发给其他计算机。由http代理提供。
假设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('/ upload')中获取文件缓冲区。 张贴或放置都没关系。 我看到当我使用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/'))
我希望您可以针对您的特定用例进行推断。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.