vue配置代理失效!

使用vue配置代理服务器时,配置了pathRewrite: {"^/api":"/"},但是貌似没有用,没有去掉/api前缀。

devServer: {
    proxy: {
      "/api": {
        target: 'http://localhost:80',
        pathRewrite: {"^/api":"/"}
      }
    }
  }

发送的请求也带了api前缀

axios.get("http://localhost:8080/api/students");

结果请求的还是带前缀/api的地址。

img

我想把/api前缀去掉,该怎么解决呢?

首页proxy配置不正确,pathRewrite: {"^/api":"/"}后面的”/“多余;
虽然在network上面看是带api的,但是不影响请求后端接口。
代理会把"**http://localhost:8080/api/**students" /api上面的所有的路径转为proxy中target的路径
即:http://localhost:80/studens
如果
pathRewrite带着"/",就相当于把http://localhost:8080/api/转为”/“,在前面拼上target上面的字段
即:http://localhost:80//studens

正确配置代理后·api·在浏览器里是可以看到的,不影响,请求本机接口不用设置代理,只有跨域才配置代理。https://blog.csdn.net/gentleman_hua/article/details/123925776

axios的话不能直接配置baseUrl

img