gpt4 book ai didi

node.js - Node的请求没有使用pipe传递POST请求

转载 作者:太空宇宙 更新时间:2023-11-04 00:34:58 25 4
gpt4 key购买 nike

使用 Node/express 编写的代理和请求无法通过管道 POST 调用,GET 似乎可以工作:

var pipeToTrustedZone = function(req, res){ 
getEnrichedHeaders(req,function(err, headers){
req.headers = headers;
var url = proxiedServerPath + req.originalUrl;
console.log(req.method+" TOWARDS");
log.info(url);
req.pipe(request({qs:req.query, uri: url })).pipe(res);
});
}

上面的代码执行 get 请求,正好作为中间件放置在快速路由器中,但是在 POST 上,proxiedServer 永远不会收到消息。

知道为什么上面的方法不起作用吗?

此外,我的应用程序正在使用主体解析器中间件,因为并非所有端点都将被代理:

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

最佳答案

您必须在传递给 request() 的对象中设置请求 headers,并在 res 上设置适当的 header 。这意味着你将无法简单地把它变成一句台词。例如:

req.pipe(request({
qs: req.query,
uri: url,
headers: req.headers
})).on('response', function(pres) {
res.writeHead(pres.statusCode, pres.headers);
pres.pipe(res);
});

关于node.js - Node的请求没有使用pipe传递POST请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39471798/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com