Skip to content

Question: Different results when using multiple proxies with react router. #2743

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

Closed
HappyZombies opened this issue Jul 7, 2017 · 6 comments

Comments

@HappyZombies
Copy link

HappyZombies commented Jul 7, 2017

So I created my react app and added react router, I have the proxy property in my package.json as so

"proxy": "http://myurl:1000"

http://myurl:1000 has an endpoint /users (so http://myurl:1000/users) that returns json data. With react router, I can create my own endpoint of the same name (http://localhost:3000/users) and I will get my component/page of users, not the JSON data.

And if I use fetch, axios, ajax etc to the endpoint of http://localhost:3000/users, I will get the json data. This all makes sense as you state in the WebpackDevServerUtils.js. However, how should I go about this for multiple proxies ?

On that same file of WebpackDevServerUtils.js and the same line, the context method is different when I specify a string (a single proxy), and the method you give for context is different when I specify an object (multiple proxies). See the code.

So I created this...

"proxy": {
    "/apiv2": {target: "http://differenturl:1001"},
    "/": {target: "http://myurl:1000"}
 }

With this, if I try to go to http://localhost:3000/users, in fact, any endpoint, it will go to the http://myurl:1000 endpoint and return me either the json data or a 404 page from the myurl:1000, not the react router page/component I have setup.

Keep in mind that http://myurl:1000 has, let's say, 20 end points. Will I have to specify each and every single one ? Or is there something I am missing/need to specify in the proxy object ? Any help with this would be appreciated, thanks!

@Timer
Copy link
Contributor

Timer commented Jul 7, 2017

Multiple proxies do not abide by the same rules, they are custom in this nature for that reason.
Why aren't you namespacing your API calls, to say, /apiv1?

@HappyZombies
Copy link
Author

HappyZombies commented Jul 7, 2017

So there is no way to have multiple proxies abide by the same rules as a single proxy in the package.json proxy property ? I tried looking for some property options in the node http proxy options but found nothing. If not it's no big deal, I can specify each endpoint in the package.json proxy property.

As for the namespacing, I was simply providing an example.

@Timer
Copy link
Contributor

Timer commented Jul 7, 2017

You'll have to specify each one unless if you can use some sort of globbing.

@Timer
Copy link
Contributor

Timer commented Jul 7, 2017

I'll close this as it's rare to have non-namespaced API (and question has been answered); if it's actually a different URL in production, you should be using an environment variable.

@Timer Timer closed this as completed Jul 7, 2017
@HappyZombies
Copy link
Author

Alright, thanks for the help, I appreciate the fast response.

@Timer
Copy link
Contributor

Timer commented Jul 7, 2017

No problem!

To expand on what I said earlier slightly, you can create an environment variable named REACT_APP_API_V1_URL and then prefix all of your API calls using process.env.REACT_APP_API_V1_URL.
e.g.

fetch(`${process.env.REACT_APP_API_V1_URL}/users`).then(...).catch(...)

If this doesn't fix your use case, I suggest namespacing your API because serving an API endpoint and then HTML page from the same route is super confusing (where you're toggling based on headers).

@lock lock bot locked and limited conversation to collaborators Jan 21, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants