Zero response through http-proxy-middleware
Asked Answered
R

3

5

I am trying to debug a proxy in Express, using http-proxy-middleware, but no matter what I do I get absolutely zero response. The endpoint sends the response but it is never returned by the proxy and the onProxyRes event is never triggered. Also the endpoint can be accessed directly through curl, Insomnia, fetch API and request-promise and is in use as an API for our customers.

Even with a setup as simple as:

app.use('/api', proxy({
  target: 'https://my.secret.endpoint',
  changeOrigin: true
});

I get absolutely zero response, except for a ECONNRESET after timeout. Any suggestions?

Resultant answered 11/9, 2018 at 7:31 Comment(0)
R
8

Heureka! I found the answer I was looking for! The offending party was (in my case) the express.urlencoded() middleware, which does some trickery that ultimately messes up the proxy. This can be resolved by applying the offending middleware after the proxy. Other middleware that might do this include cookie-parserand bodyParser (both of which are deprecated).

More on the issue here: https://github.com/chimurai/http-proxy-middleware/issues/40

Resultant answered 11/9, 2018 at 12:49 Comment(2)
Same to me. This also explains why it only happened to me on POST request with a request body, but not on a regular GET request. Thank you so much.Oran
This saved me many more hours, I spent on it the entire dayAvail
P
3

ViggoV
You need to disable the SSL when the target is https.

Like as this:

app.use('/api', proxy({
  target: 'https://my.secret.endpoint',
  changeOrigin: true,
  secure: false
});
Pineal answered 28/9, 2018 at 9:30 Comment(1)
As you can see from my own answer I managed to fix it by other means. However it somehow works without secure: false, but it may be because both projects (api backend and express server) run within the same secure network(?). I'll definitely keep it in mind if I run into trouble when we take it into production. Thanks for the input in any case :)Resultant
R
2

Another solution for anyone coming here with the same issue but did not solve it.

In proxy configuration make sure you are matching any path with double ** not only *

const proxy = require("http-proxy-middleware");
module.exports = function(app) {
  app.use(proxy("/api/**", { // https://github.com/chimurai/http-proxy-middleware
    target: "http://localhost:5000",
    secure: false
  }));
};

For more check this reference

Radke answered 7/4, 2019 at 16:9 Comment(1)
Yes. This was my issue - after setting context to /**, as opposed to /*, the proxy finally started forwarding requests.Parsley

© 2022 - 2024 — McMap. All rights reserved.