JS:向特定端口发送http请求

发布于 2025-01-19 01:40:31 字数 231 浏览 3 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

幸福还没到 2025-01-26 01:40:31

您可以创建一个类似的Axios实例:

export const myInstance = axios.create({
  baseURL: backendUrl,
})

后端应该是后端的环境变量: https://production.com 或http:// localhost:4000

You can create an axios instance like so:

export const myInstance = axios.create({
  baseURL: backendUrl,
})

backendUrl should be an environment variable to your backend: https://production.com or http://localhost:4000

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文