Reputation: 9686
Proxy written using node/express and request fails to pipe POST calls, GET seems to work:
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.
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 }));
Upvotes: 1
Views: 1208
Reputation: 106698
You have to both set request headers
in the object passed to request()
and set the appropriate headers on res
. 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);
});
Upvotes: 1