简体   繁体   English

反应对hostedVM上的localhost节点服务器的应用调用

[英]React app call to localhost node server on hostedVM

I'm serving a built react file using Nginx . 我正在使用Nginx提供一个内置的react文件。 I am able to access the react app in the browser on my vm's IP address. 我可以在虚拟机的IP地址上的浏览器中访问react应用程序。

I have another server running on my VM on port 8080 , the API of the react app. 我在我的VM的端口8080上运行了另一台服务器,React应用程序的API。

In my react I use axios calls like 在我的反应中,我使用axios呼叫,例如

const http = "http://localhost:8080";
..
axios.post(`${http}/api/auth/login`, { credentials }).then(res => res.data.user),

In my browser after I access my VM's ip address I can see in the console that the so called axios calls go to http://myActualVMIpAddress/api/.. instead of the localhost path as I was expecting. 访问虚拟机的IP地址后,在浏览器中,我可以在控制台中看到所谓的axios调用转到http://myActualVMIpAddress/api/..而不是我期望的localhost路径。

I have checked the routes using postman of the api server. 我已经使用api服务器的postman检查了路由。

I don't have experience with deployment. 我没有部署经验。 How do I make the link between the react app and the api server to work? 如何使React App和api服务器之间的链接正常工作?

It is not a good idea to hard code localhost:8080 into your client JS, when your user loads the client files they will try and make a request on your users computer which is what localhost resolves to. 将localhost:8080硬编码到客户端JS中不是一个好主意,当用户加载客户端文件时,他们将尝试在用户计算机上发出请求,这是localhost解析的。

Easiest is to serve your client files from your api server. 最简单的是从api服务器提供客户端文件。 If you want to keep these separate then you have to use the IP of your API server (not localhost) and setup CORS. 如果要将它们分开,则必须使用API​​服务器(而不是localhost)的IP并设置CORS。

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

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