How to conditionally proxy request?#167

Labels

Milestone

Assignee

4 participants

I'd like to proxy requests to an API backend if the Accept header matches a certain regex. If it doesn't match, no proxying is done and instead a static page is served with Connect's static middleware. I saw that createServer allows me to either pass in middleware, or to pass in a function to modify before proxying, but I don't see how to conditionally proxy based on request attributes.

How would one go about writing this? In pseudocode, I'd want something like:

I'd like to use a URL approach also: handle requests to certain URLs with the primary app, but proxy certain URLs to a separate app. It seems from the examples that the components to do all this exist, but I can't get them to work together. Creating a 3rd server seems redundant... does anyone know another way? Thanks!

@jch does your example not work for you? If you don't call next in your middleware, it won't pass it on to the default handler anyways (next is a function passed into the third parameter in the middleware).

@CodeRarity I ended up with a different architecture, so I no longer needed to proxy requests. It was more trouble than it was worth, so now I have my API and static web endpoints mounted together at the same place.