Reputation: 738
I created a react app on my MacBook and I access it with http://localhost:3000
.
Additionally I run an nodejs express mysql server on http://localhost:5000
.
When I open the IP address with port 3000 in the browser of my Windows 10 laptop in the same network (http://192.166.15.85:3000
) I see my app, but the console throws an network error for my backend, because it can not access http://localhost:5000
.
I hard coded the axios.get()
in the frontend to call http://localhost:5000
, what is fine for my MacBook. But for the Windows 10 laptop I'd need the ip address instead.
How can I handle this?
The error is:
GET http://localhost:5000/get_users net::ERR_CONNECTION_REFUSED
Error: Network Error
at: createError(createError.js:16)
at: XMLHttpRequest.handleError (xhr.js:91)
Upvotes: 2
Views: 1273
Reputation: 366
You can get the current hostname by using window.location.hostname
and then conditionally change the url
Upvotes: 2