Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to setup Koa reverse proxy to all “/api” requests to by-pass CORS communication with API server? #455

Closed
gschema opened this issue Jun 24, 2015 · 12 comments
Labels

Comments

@gschema
Copy link

gschema commented Jun 24, 2015

Hi guys,
Is there any good example or documentation for this type of thing?
Currently I cannot setup CORS on API server reverse proxying API calls is only option.

Reverse proxy setup should proxy all calls to the /api and should not trim headers standard and custom both ways.

Application is Node app build with Koa, React, Flummox, React router etc.

Thanks

@cesarandreu
Copy link

I'm usually on IRC and willing to help. There's also a the mailing list.
Github issues is not really the best place to ask questions.

Try koa-cors?

@jedrichards
Copy link

Does this help? https://www.npmjs.com/package/koa-pixie-proxy

@gschema
Copy link
Author

gschema commented Jun 27, 2015

@cesarandreu tnx man. I'll reach out.
How I understand that koa-cors solves different problem (enabling CORS on API side; correct my if I am missing something), while my goal is to enable my isomorphic app on client to be able to communicate with already built API (which I have no control of and that one has no enable CORS settings) so I have to reverse proxy API requests through my node app server side.

@jedrichards I totally had no luck with that one or koa-proxy or koa-http-proxy. Actually with any solution I found online (that's why I posted here, I guess it would be of a benefit for others too).

@cesarandreu
Copy link

Oh, I see. Previously I've wrapped node-http-proxy for one of my apps... But it's a bit different.

But for your problem, it seems like CORS-Proxy and cors-anywhere would be good references for making your own wrapper.

@jedrichards
Copy link

I'd be interested to hear how you get on with this. I'm writing an app with a very similar tech stack (isomorphic React, Flux, Koa). At the moment I'm writing my REST API endpoints in Koa, but eventually I could see needing to separate the API out, at which point I'll have exactly the same problem as you.

There seems to be relevant discussion in this issue: #198

Basically seems to be using node-http-proxy directly in Koa, and setting this.respond = false to allow the proxy to respond instead of Koa, somehow?

@jonathanong
Copy link
Member

can we close this?

like i said #198 (comment), i would actually not use koa for proxying - use something closer to node's API.

also, i'm not exactly sure what the problem is.

@hallas
Copy link
Contributor

hallas commented Jun 29, 2015

I'm not sure what the problem is either. I don't see why there can be any koa-specific problems to proxy and cors :)

@g6scheme Let us know if this can be closed

@jedrichards
Copy link

Is #198 (comment) a fairly complete example? It looks good to me, but how do we then make use of the this.response flag? Also usage of that flag seems fraught with some sort of danger, but it isn't clear what that danger is to people more naive about Koa from the slightly opaque discussion that comes after.

I think as people make more isomorphic apps, needing a thin Node-based static file server and isomorphic app renderer will be a very common requirement that people will gravitate towards Koa for. It'd be nice to either have a super clear example of API request proxying on a Koa route, or some middleware that worked out of the box.

If #198 (comment) is that example I would say this issue is close-able :)

@gschema
Copy link
Author

gschema commented Jun 29, 2015

Thanks guys.
Closing it.

@gschema gschema closed this as completed Jun 29, 2015
@bdefore
Copy link

bdefore commented Jul 2, 2015

@jedrichards i agree and would like to see a more fleshed out example. setting up an isomorphic react app that proxies /api to another host is exactly the use case that's led me to this issue (and koa-proxy does not appear to provide this functionality). the example provided in the comment, a year and a half old, isn't very complete and there's no context behind why i'm hanging my proxy on a callback for koa.

@ngduc
Copy link

ngduc commented Apr 19, 2016

I'd love to see updates.
I haven't found an example for this online. Thanks.

@PlasmaPower
Copy link
Contributor

@ngduc I'm on my phone but try this:

app.use(mount('/api', cors()));

Where mount is koa-mount and cors is koa-cors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

8 participants