Vue的代理配置pathRewrite重写不生效问题处理
本人遇到的情况在网上搜索时没有看到本人遇到的情况,所以特此记录一下,希望能帮助到你。
devServer: {
proxy: {
//代理匹配前缀2
'/api2': {
target: 'http://localhost:5001',
pathRewrite: {'^/api2': ''},
ws: true,
changeOrigin: true
},
//代理匹配前缀1
'/api': {
target: 'http://localhost:5000',
pathRewrite: {'^/api': ''},
ws: true,)
changeOrigin: true
}
}
}
开始时配置了/api前缀的代理,没有问题,然后又配置了/api2前缀的配置代理,然后就无法访问/api2前缀的代理了。
原因: 就是因为/api在前面,先匹配了/api,例如访问/api2/test,由于先匹配了/api所以重写/api为空字符串后,路径变为了2/test,自然就不对了
解决:把/api2放在/api前面,让api2先匹配到,或者取名叫/api2,让其无法先匹配到/api