這篇文章主要介紹“怎么使用vue項(xiàng)目配置多個代理”,在日常操作中,相信很多人在怎么使用vue項(xiàng)目配置多個代理問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么使用vue項(xiàng)目配置多個代理”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
目前成都創(chuàng)新互聯(lián)公司已為上千多家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)絡(luò)空間、成都網(wǎng)站托管、企業(yè)網(wǎng)站設(shè)計(jì)、永豐網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
在Vue項(xiàng)目的開發(fā)過程中,為了本地調(diào)試方便,我們通常會在 vue.config.js 中配置 devServer 來在本地啟動一個服務(wù)器,在這個選項(xiàng)中,我們會配置proxy 屬性來將指向到本地的請求(例如: /api/action) 代理到后端的開發(fā)服務(wù)器上(例如: http://xxx.xxx.xxx/api/action)
devServer: {
port: 8081,
proxy: {
'/api/action': {
target: 'http://192.168.200.106:81',
changeOrigin: true,
ws: true,
secure: false
}
}
},
```接口地址有重疊地址時(shí),將匹配度低的放在后面。
例如:
* 將 / 匹配到 192.191.1.1;
* 將 /api 匹配到 192.191.1.2
* 將 /api/action 匹配到 192.191.1.3
如果我們像下面一樣書寫:
proxy: {
'/': {
target: 'http://192.191.1.1',
changeOrigin: true,
ws: true,
secure: false
},
'/api': {
target: 'http://192.191.1.2',
changeOrigin: true,
ws: true,
secure: false
},
'/api/action': {
target: 'http://192.191.1.3',
changeOrigin: true,
ws: true,
secure: false
}
}那么所有到/, /api和 /api/action 的請求將全部被代理到 192.191.1.1 上面去
原因是這里的匹配實(shí)際上是一個正則匹配的過程,當(dāng)我們請求 /api 時(shí),首先讀取到了配置項(xiàng)中的第一個,拿配置中的 / 去匹配請求中的 /api , 發(fā)現(xiàn)請求的/api 中包含配置項(xiàng)/, 匹配成功,直接將請求代理到了 192.191.1.1 上面去, 對/api/action的匹配也同理。
也就是說,它的匹配規(guī)則是: 拿配置項(xiàng)中的地址去匹配請求中的地址,如果請求中的地址中包含配置中的地址,則匹配成功,否則,拿下一個配置項(xiàng)繼續(xù)匹配。
所以,配置中的地址與請求地址中匹配的字符越少,匹配度越低。 上例中配置中的地址(/)與請求地址(/api)只有一個字符是匹配的,所以匹配度低。
所以我們正確的寫法應(yīng)該是:
proxy: {
'/api/action': {
target: 'http://192.191.1.3',
changeOrigin: true,
ws: true,
secure: false
},
'/api': {
target: 'http://192.191.1.2',
changeOrigin: true,
ws: true,
secure: false
},
'/': {
target: 'http://192.191.1.1',
changeOrigin: true,
ws: true,
secure: false
}
}這樣到三個地址的請求就都可以正確代理到相應(yīng)的地址去了
在實(shí)際應(yīng)用中,由于后端采用微服務(wù)模式開發(fā),在開發(fā)階段,我們可能會將不同的服務(wù)代理到不同的地址上,當(dāng)服務(wù)很多時(shí),我們代理的數(shù)量也就很多:
proxy: {
'/api/action': {
target: 'http://192.191.1.3',
changeOrigin: true,
ws: true,
secure: false
},
'/api/action2': {
target: 'http://192.191.1.4',
changeOrigin: true,
ws: true,
secure: false
},
'/api/action3': {
target: 'http://192.191.1.3',
changeOrigin: true,
ws: true,
secure: false
},
'/api/action4': {
target: 'http://192.191.1.4',
changeOrigin: true,
ws: true,
secure: false
},
'/api/action5': {
target: 'http://192.191.1.5',
changeOrigin: true,
ws: true,
secure: false
},
'/api/action6': {
target: 'http://192.191.1.6',
changeOrigin: true,
ws: true,
secure: false
},
'/api/action7': {
target: 'http://192.191.1.5',
changeOrigin: true,
ws: true,
secure: false
},
'/api/action8': {
target: 'http://192.191.1.6',
changeOrigin: true,
ws: true,
secure: false
},
'/api/action9': {
target: 'http://192.191.1.7',
changeOrigin: true,
ws: true,
secure: false
},
'/api': {
target: 'http://192.191.1.2',
changeOrigin: true,
ws: true,
secure: false
},
'/': {
target: 'http://192.191.1.1',
changeOrigin: true,
ws: true,
secure: false
},
}當(dāng)配置的代理數(shù)量超過十個時(shí),開發(fā)環(huán)境編譯打包時(shí)會報(bào)以下錯誤:

為了解決報(bào)錯,也同時(shí)減少代碼體積,我們可以對具有同一個target的配置項(xiàng)進(jìn)行合并,由上文我們可知,這里其實(shí)是一個正則匹配的過程,那我們就可以利用正則語法將他們進(jìn)行合并:
proxy: {
'/api/action|/api/action3': {
target: 'http://192.191.1.3',
changeOrigin: true,
ws: true,
secure: false
},
'/api/action2|/api/action4'': {
target: 'http://192.191.1.4',
changeOrigin: true,
ws: true,
secure: false
},
'/api/action5|/api/action7': {
target: 'http://192.191.1.5',
changeOrigin: true,
ws: true,
secure: false
},
'/api/action6|/api/action8': {
target: 'http://192.191.1.6',
changeOrigin: true,
ws: true,
secure: false
},
'/api/action9': {
target: 'http://192.191.1.7',
changeOrigin: true,
ws: true,
secure: false
},
'/api': {
target: 'http://192.191.1.2',
changeOrigin: true,
ws: true,
secure: false
},
'/': {
target: 'http://192.191.1.1',
changeOrigin: true,
ws: true,
secure: false
},
}當(dāng)然,在正式部署的時(shí)候,還是需要后端去做統(tǒng)一代理。
到此,關(guān)于“怎么使用vue項(xiàng)目配置多個代理”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
網(wǎng)站題目:怎么使用vue項(xiàng)目配置多個代理
當(dāng)前地址:http://www.chinadenli.net/article8/gcciip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、用戶體驗(yàn)、網(wǎng)站設(shè)計(jì)、電子商務(wù)、定制網(wǎng)站、微信公眾號
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)