切换到vite之后,我正在尝试模仿以前在proxy: "http://localhost:5000"
中使用的package.json
。
这是我的vite配置
export default defineConfig({
plugins: [react()],
server: {
proxy: {
"/api": {
target: "http://localhost:5000",
changeOrigin: true,
secure: false,
},
},
},
});
我有反应应用程序运行在端口3000。当我在根url (http://localhost:3000
)中发送请求时,一切正常
const { data } = await axios.get("api/user/me");
好吧,
http://localhost:3000/api/user/me
而不是http://localhost:5000/api/user/me
。有人能解释一下这种行为吗?主要问题是,当我导航到另一个页面(例如http://localhost:3000/dashboard
)时,相同的请求将被发送到http://localhost:3000/dashboard/api/user/me
。
我做错了什么?我想向http://localhost:5000
发送请求,不管位置如何
我找到了一个解决办法,在每个请求const { data } = await axios.get("http://localhost:3000/api/user/me");
之前指定FE url,但是仍然有一种模仿package.json
代理行为的方法吗?
发布于 2022-05-18 15:38:37
我通过更改axios默认值来解决这个问题。
axios.defaults.baseURL = `http://localhost:5000`
通过这样做,我实现了我想要的目标。请求被发送到适当的端点,而不管位置如何。
https://stackoverflow.com/questions/72266050
复制相似问题