简体   繁体   English

如何在本地网络中使用节点后端访问我的 React 应用程序?

[英]How to access my React app with node backend in local network?

I created a react app on my MacBook and I access it with http://localhost:3000 .我在 MacBook 上创建了一个 React 应用程序,并使用http://localhost:3000访问它。 Additionally I run an nodejs express mysql server on http://localhost:5000 .此外,我在http://localhost:5000上运行 nodejs express mysql 服务器。 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 .当我在同一网络 ( http://192.166.15.85:3000 ) 中的 Windows 10 笔记本电脑的浏览器中使用端口 3000 打开 IP 地址时,我看到了我的应用程序,但控制台为我的后端抛出了一个网络错误,因为它无法访问http://localhost:5000 I hard coded the axios.get() in the frontend to call http://localhost:5000 , what is fine for my MacBook.我在前端硬编码axios.get()以调用http://localhost:5000 ,这对我的 MacBook 来说很好。 But for the Windows 10 laptop I'd need the ip address instead.但是对于 Windows 10 笔记本电脑,我需要 IP 地址。 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)

您可以使用window.location.hostname获取当前主机名,然后有条件地更改 url

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM