http-party / node-http-proxy

A full-featured http proxy for node.js
https://github.com/http-party/node-http-proxy
Other
13.92k stars 1.97k forks source link

body parser before proxy request #791

Open nguyenxuantuong opened 9 years ago

nguyenxuantuong commented 9 years ago

I am wondering is there anyway to use body parse before proxy request. Here is my scenario: app.use(bodyParser.json()) app.use(bodyParser.urlencoded({ extended: false }));

proxy.on('proxyReq', function(proxyReq, req, res, options) { //Need to write custom logic to check for the parameter inside the body. -- so that it can add custom header or modify proxyReq body, etc... //for example: var userId = req.param("userId") -> this one only available with bodyParser declared //before this one (see abover) }); app.all(route, function(req, res){ //proxy request to target url return proxy.web(req, res, { target: config.localUrl + "/api"}, function(e) { return res.json(501, e.stack || e.error || e); }); });

The web application at target also uses its own bodyParser. But it seems that because the body parser being used twice, no response arrived after proxy the request to target. I am wondering if there is any way to work around that issue? Thank.

lchenay commented 5 years ago

Please see : https://github.com/nodejitsu/node-http-proxy/blob/master/examples/middleware/bodyDecoder-middleware.js