Skip to content

配置代理服务器

方法一

JavaScript
devServer: {
  proxy:'http://localhost:5000'
}

缺陷,当publish文件夹中存在请求的文件时,便不会代理请求

方法二

能同时配置多个代理以及选择是否使用代理

javascript
devServer: {
    proxy: {
      '/api': {
        target: 'http://localhost:5000',
        // 重写请求路径去掉api前缀
        pathRewrite:{"^/api":""},
        // 用于支持websocket
        // ws: true,
        // 用于控制请求头中的host值
        // changeOrigin: true
      },
      '/demo': {
        target: 'http://localhost:5001',
        pathRewrite:{"^/demo":""},
      }
    }
  }

methods代码

JavaScript
methods:{
    getStudent(){
        axios.get("http://localhost:8080/api/students").then(
            response => {
                console.log("请求成功了",response.data);
            },
            error => {
                console.log(error.message);
            }
        )
    },
    getCar(){
        axios.get("http://localhost:8080/demo/cars").then(
            response => {
                console.log("请求成功了",response.data);
            },
            error => {
                console.log(error.message);
            }
        )
    }
}