Skip to content

API路径问题 #1031

Answered by pnck
quixote-liu asked this question in Q&A
Discussion options

You must be logged in to vote

那个/api/是proxy出来的,通常为了防止同源问题会用 nginx(dev时用vite)把后端的地址代理到前端server的host下。

举个例子:

  • 后端server: localhost:8888
  • 前端server: localhost:8080

此时在浏览器访问时,浏览器请求是发给 localhost:8080的。如果不配置代理,那么发起ajax时,浏览器直接去请求 localhost:8888 就会收不到数据,因为跨域了。因此用vite或nginx把 localhost:8888代理到 localhost:8080下:

  • 前端server: localhost:8080/
  • 后端server:localhost:8080/api/

此时前后端在同一个域下就没有同源问题了。
参考:

VITE_BASE_API = /api

VITE_BASE_API = /api

注意这个rewrite:
rewrite: path => path.replace(new RegExp('^' + process.env.VITE_BASE_API), ''),


Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by pnck
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants