配置setproxy代理是常用操作,但是一般会配置通用代理,如果说项配置多个代理,那么要如何配置和使用,话不多说,上代码
const proxy = require('http-proxy-middleware').createProxyMiddleware
module.exports = function (app) {
app.use(
proxy('/aaa', {
//自行带入自己的代理地址和端口
target: 'http://x.x.x.x:1001',
changeOrigin: true,
pathRewrite: {
'^/aaa': ''
},
}),
proxy('/bbb', {
target: 'http://x.x.x.x:1002',
changeOrigin: true,
pathRewrite: {
'^/bbb': ''
},
}),
)
}
api.js中使用代理
//此处的代理为/aaa,即配置的首个代理
export const api =()=>{
return axios({
url: `/api`,
method: 'get'
})
}
//使用配置项第二代理/bbb
export const api2 =()=>{
return axios({
baseURL:'/bbb',
//需要携带token的话,不需要删除
headers:{Authorization: `${token}`},
url: `/api2`,
method: 'get'
})
}
三个四个以此类推就可以了,希望能帮到在查找这个解决办法的小伙伴。