In short, we're using https://github.com/chimurai/http-proxy-middleware and if the initial proxied endpoint returns a 404 or 500, we'd like to lead the request to a different endpoint instead.
i.e.
-> http://www.example.com (404)
-> http://www.backup.com (200)
So the client receives a response from www.backup.com.
                        
After some investigation, I find it's impossible to lead the HTTP request to another endpoint, using http-proxy-middleware.
http-proxy-middleware is powered by node-http-proxy and it's impossible to implement the requirement in node-http-proxy. The related code is listed below:
Once the response is received, an event
proxyResis emitted, and that response is immediately piped to browser (resobject).Although we can listen
proxyResevent and know the response status is404or500, there is nothing we can do to change the response itself.To implement the requirement, you need to write the proxy logic yourself, or use some other proxy module which support resonse-interception.