Reputation: 2173
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?
Upvotes: 5
Views: 7682
Reputation: 1134
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
Upvotes: 2
Reputation: 31
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
});
Upvotes: 3
Reputation: 2173
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-parser
and bodyParser
(both of which are deprecated).
More on the issue here: https://github.com/chimurai/http-proxy-middleware/issues/40
Upvotes: 8