写的配置多个代理如下代码,官方地址
module.exports = {
devServer: {
proxy: {
'/api': {
target: '',
ws: true,
changeOrigin: true,
pathRewrite: { //我自己添加,路径重写
'^api': ''
}
},
'/foo': {
target: ''
}
}
}
}
确实还不够用,还要配置pathRewrite,有些还要好好解释一下
ws: true, //代理websockets
changeOrigin: true, // 是否跨域,虚拟的站点需要更管origin
pathRewrite: 这部分是路径重写,因为上面前端请求接口多增加 api,需要匹配到时,删除api,这个用来匹配代理的字符串
关于具体pathRewrite 路径重写知识如下图,具体官网地址
背景:访问百度开放地图接口,所以前端必须要代理,不然就会产生跨域
//App.vue
//vue.config.js
module.exports = {
devServer: {
proxy :{
'/api':{
target: "https://api.map.baidu.com",
ws: true, //代理websockets
changeOrigin: true, 是否跨域,虚拟的站点需要更管origin
pathRewrite: {
'^/api': ''
}
},
'/test':{
target: "https://api.map.baidu.com",
ws: true,
changeOrigin: true,
pathRewrite: {
'^/test': ''
}
}
}
}
}
3. 解释多个代理匹配的问题
在浏览器中看到的结果:api其实还在,其实访问是正确的,因为这里就是这样,隐藏真实访问路径( 真实访问路径 /place/v2/search/.. )



