http-proxy-middleware icon indicating copy to clipboard operation
http-proxy-middleware copied to clipboard

How to "next" (skip middleware) based on the response?

Open gajus opened this issue 7 years ago • 8 comments

I have a setup such as:

app.use('/', proxy({
  changeOrigin: true,
  target: PRERENDERED_APP_URL
}));
app.use('/', proxy({
  changeOrigin: true,
  target: APP_URL
}));

If PRERENDERED_APP_URL responds with an error (e.g. status 404), I want to delegate the request to the next route. I do not see "next" as an option in the onProxyRes.

gajus avatar Jul 05 '17 07:07 gajus

Do you mean you want to proxy to APP_URL when proxying to PRERENDERED_APP_URL fails (with 404)?

chimurai avatar Jul 05 '17 20:07 chimurai

Yes

gajus avatar Jul 05 '17 20:07 gajus

I'm gonna look into it tomorrow because I have quite the same need: I need to be able to use middlewares for processing the proxy's response, or having a default one that will send the response as it is.

gogson avatar Jul 06 '17 15:07 gogson

@gogson Did you have a chance to look into this?

gajus avatar Jul 10 '17 19:07 gajus

@gajus I'm afraid I can't do anything without modifying the http-proxy module itself. I keep you posted.

gogson avatar Jul 11 '17 14:07 gogson

@gajus I managed to do my thing I made a document about my solution. I didn't have much time to do that so it's quick'n'dirty but it works for my case: how to take a JSON response from the proxy and modify it before answering the request using route middlewares.

I'm not sure it will help you but you can look at my solution it may give you ideas. Don't hesitate to mail me if you need some hints.

gogson avatar Jul 11 '17 17:07 gogson

Do you have any idea? @chimurai

I just want to do sth after finish proxying, looking forward to your reply.

NoraGithub avatar May 03 '18 09:05 NoraGithub

@chimurai some news here?

If you want I can work on it, this feature its very important for us to allow our mock server to have a fallback in case that the proxied server returns some 4XX or 5XX http codes

rafa-suagu avatar Dec 15 '18 17:12 rafa-suagu