简体   繁体   English

节点的请求没有使用管道传递 POST 请求

[英]Node's request does not pass POST requests using pipe

Proxy written using node/express and request fails to pipe POST calls, GET seems to work:使用 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);
    });
}

The above code executes get requests just fine placed as a middleware in an express router, however on POST the proxiedServer never receives the message.上面的代码执行 get 请求,作为中间件放置在 express 路由器中,但是在 POST 上,proxyedServer 永远不会收到消息。

Any idea why the above does not work?知道为什么上述方法不起作用吗?

Also my app is using body parser middlewares since not all endpoints are to be proxied:此外,我的应用程序正在使用正文解析器中间件,因为并非所有端点都将被代理:

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

You have to both set request headers in the object passed to request() and set the appropriate headers on res .您必须在传递给request()的对象中设置请求headersres上设置适当的标头。 That means you won't be able to simply make it one-liner.这意味着您将无法简单地使其成为单线。 For example:例如:

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

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

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