JS:向特定端口发送http请求
我有一个MERN网站,该网站使用端口80用于前端,后端API的端口4000。在MERN教程中,他们将axios.post('localhost:4000/login'...)
与后端API通信。但是,如果我有一个像example.com
这样的域,这将在生产中起作用。有没有一种方法可以使用相对URL指定端口,例如axios.post(':4000/login')
?
I have a MERN website that uses port 80 for the front-end and port 4000 for the back-end API. In MERN tutorials, they put axios.post('localhost:4000/login'...)
to communicate with the backend API. But this won't work in production if I have a domain like example.com
. Is there a way to use relative URLs to specify a port, like axios.post(':4000/login')
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以创建一个类似的Axios实例:
后端应该是后端的环境变量: https://production.com 或http:// localhost:4000
You can create an axios instance like so:
backendUrl should be an environment variable to your backend: https://production.com or http://localhost:4000