create-react-app icon indicating copy to clipboard operation
create-react-app copied to clipboard

Proxy to external API

Open stinaq opened this issue 10 years ago • 15 comments

In the README there are instructions on how to set up a proxy for API requests in development. I have followed those instructions and it does not work as expected.

Any unrecognized request will be redirected to the specified proxy.

I have set up my proxy like so:

"proxy": "http://test.example.com/"

In the packaage.json and this eliminates the CORS error I get before I added the proxy config, but the request to the API is not working. In a component I am doing a fetch() to the address /api/recent.json which I would then expect to result in http://test.example.com/api/recent.json and I have tested that address and it works, but I get a 404 in the web console, saying http://localhost:3000/api/recent.json can't be found.

Am I missing something or should this not work?

stinaq avatar Aug 24 '16 21:08 stinaq

Regarding the subject: Is there a way to update an existing app created with react-create-app 0.2.0 to version 0.3.0 ?

javlc avatar Aug 24 '16 21:08 javlc

Hi! Sorry for the trouble. There is a note above this section in README:

Note: this feature is available with [email protected] and higher.

You are likely using 0.2.1 because it is the latest stable version. Please wait for 0.3.0 to come out.

gaearon avatar Aug 24 '16 23:08 gaearon

Is there a way to update an existing app created with react-create-app 0.2.0 to version 0.3.0 ?

This question is answered here: https://github.com/facebookincubator/create-react-app/issues/485#issuecomment-242229575.

gaearon avatar Aug 24 '16 23:08 gaearon

I have now also tried setting up a simple HTTP Python server locally so I can monitor requests to that, and the proxy now looks like this:

"proxy": "http://localhost:8000"

And the request I do from the app looks like this:

fetch('/api', {
  method: 'get'
}).then(function(response) {
  console.log(response);
}).catch(function(err) {
  console.log(err);
});

And now I get the response:

type: "basic", url: "http://localhost:3000/api", status: 200, ok: true, statusText: "OK"

But monitoring the :8000 local server, no requests at all comes through, so it does not proxy the requests though there.

stinaq avatar Aug 25 '16 06:08 stinaq

Ok, that's too bad. I didn't check the version, just assumed 0.3.0 vas the latest. Perhaps add that at the top of the instructions? =)

stinaq avatar Aug 25 '16 06:08 stinaq

README generated in your project shouldn't have this section. I amended the one in master to clarify this: f1b6168a6b758701efd680d3c99e0bf127a2ee1d. Do you think this helps, or is there a better way to phrase it?

gaearon avatar Aug 25 '16 10:08 gaearon

By the way, you can use 0.3.0-alpha if you really want this feature right now. To do that, you would need to set react-scripts in your package.json to 0.3.0-alpha and run npm install. Then the proxy field will work.

gaearon avatar Aug 25 '16 10:08 gaearon

@gaearon That amendment is very good, makes it a whole lot clearer Will that proxy work against an external domain, like http://api.example.com, or only towards a localhost address?

stinaq avatar Aug 25 '16 11:08 stinaq

It should work against a remote domain.

Actually I think I'll go ahead and backport this to 0.2.x since many people are asking for it, and 0.3.0 is a bit delayed.

gaearon avatar Aug 25 '16 11:08 gaearon

@gaearon I can try it out against an external url as soon as you have! Or if you can point me to what I should change in order to try it out.

stinaq avatar Aug 25 '16 12:08 stinaq

@gaearon I had to add changeOrigin: true to the config to make it work for an external address =)

stinaq avatar Aug 25 '16 12:08 stinaq

Hmm, it should be there, is it not?

gaearon avatar Aug 25 '16 13:08 gaearon

0.2.3 is released and contains this. Please see the instructions here.

If something doesn’t work please file an issue, preferably with a project reproducing it. Thanks!

gaearon avatar Aug 25 '16 13:08 gaearon

@gaearon thank you, I missed it!

stinaq avatar Aug 26 '16 08:08 stinaq

Hi! I have some questions about this and I'm a bit lost.

I know how to set the proxy setting in package.json so I can fetch from an external API, but...

  • What happens if I use more than one API in my app?
  • Using proxy, I don't have to specify the whole URL in my fetch request, but afaik this only works in development. What's the best way to handle this for production builds using the same code?

Thanks!

dani-mp avatar Mar 12 '17 18:03 dani-mp