4

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?

ViggoV
  • 1,801
  • 2
  • 18
  • 22

3 Answers3

5

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

ViggoV
  • 1,801
  • 2
  • 18
  • 22
  • 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. – Filipiz Sep 08 '20 at 14:05
1

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

Ahmed Younes
  • 494
  • 5
  • 12
0

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
});
EstevaoLuis
  • 1,902
  • 7
  • 28
  • 35
  • 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 :) – ViggoV Sep 28 '18 at 13:02