Open DanielHeckrath opened 8 years ago
We are having the same issue using the proxy with body-parser
@DanielHeckrath, I believe you are correct. The only solution I know of is to register your proxy middleware before the bodyParser middleware.
app.post('/api/v1/device/register', requestProxy({
url: "http://localhost:3000/api/v1/user/activation"
}));
app.use(bodyParser.json());
// other middleware
@dvonlehman there is always the possibility to add an option to ignore the request body. I added said flag in https://github.com/DanielHeckrath/express-request-proxy/commit/bac3970d82368a81b04fd616fc9a34a9ff02f91a as a hotfix for a project I'm currently working on.
While this solution works I don't really like it very much. I think there might be a solution that is slightly more flexible. There exists a PR for https://github.com/cesine/express-http-proxy/commit/714874daa2780820cc8a98334c2ebfac4b682cb8 with a proposed solution for the same problem. It should be possible to handle cases where req.body is set by the body parser if it has consumed the request body and redirect this body to the proxy target. I believe there are some straightforward cases where the body can be transmitted pretty much as is, like if the body is an object and the request content type was application/json.
I think there might be an issue with other express middlewares that consume the request body like body-parser.
I believe that after body-parser consumes the request body it's no longer possible to pipe the request body to the new proxy request like it's done in proxy.js because it will no longer emit an end event.
I think this might also be the underlying issue for #6 where request timeouts are observed on POST requests.