Zero response through http-proxy-middleware

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?

Answers:

Answer

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

Answer

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
});
Answer

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

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.