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
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?
Does this help? https://www.npmjs.com/package/koa-pixie-proxy
@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).
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.
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: https://github.com/koajs/koa/issues/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?
can we close this?
like i said https://github.com/koajs/koa/issues/198#issuecomment-33208283, 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.
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
Is https://github.com/koajs/koa/issues/198#issuecomment-33208283 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 https://github.com/koajs/koa/issues/198#issuecomment-33208283 is that example I would say this issue is close-able :)
Thanks guys.
Closing it.
@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.
I'd love to see updates.
I haven't found an example for this online. Thanks.
@ngduc I'm on my phone but try this:
app.use(mount('/api', cors()));
Where mount is koa-mount and cors is koa-cors.