使用vue項目配置多個代理的注意點
在Vue項目的開發(fā)過程中,為了本地調試方便,我們通常會在 vue.config.js 中配置 devServer 來在本地啟動一個服務器,在這個選項中,我們會配置proxy 屬性來將指向到本地的請求(例如: /api/action) 代理到后端的開發(fā)服務器上(例如: 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
}
}
},
```
在這個配置中,要注意以下兩點:
接口地址有重疊地址時,將匹配度低的放在后面。
例如:
- * 將 / 匹配到 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 上面去
原因是這里的匹配實際上是一個正則匹配的過程,當我們請求 /api 時,首先讀取到了配置項中的第一個,拿配置中的 / 去匹配請求中的 /api , 發(fā)現(xiàn)請求的/api 中包含配置項/, 匹配成功,直接將請求代理到了 192.191.1.1 上面去, 對/api/action的匹配也同理。
也就是說,它的匹配規(guī)則是: 拿配置項中的地址去匹配請求中的地址,如果請求中的地址中包含配置中的地址,則匹配成功,否則,拿下一個配置項繼續(xù)匹配。
所以,配置中的地址與請求地址中匹配的字符越少,匹配度越低。 上例中配置中的地址(/)與請求地址(/api)只有一個字符是匹配的,所以匹配度低。
所以我們正確的寫法應該是:
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
}
}
這樣到三個地址的請求就都可以正確代理到相應的地址去了
多個地址代理同一個target 時,可進行合并
在實際應用中,由于后端采用微服務模式開發(fā),在開發(fā)階段,我們可能會將不同的服務代理到不同的地址上,當服務很多時,我們代理的數量也就很多:
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
},
}
當配置的代理數量超過十個時,開發(fā)環(huán)境編譯打包時會報以下錯誤:

為了解決報錯,也同時減少代碼體積,我們可以對具有同一個target的配置項進行合并,由上文我們可知,這里其實是一個正則匹配的過程,那我們就可以利用正則語法將他們進行合并:
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
},
}
當然,在正式部署的時候,還是需要后端去做統(tǒng)一代理。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
vue創(chuàng)建項目卡住不動,vue?create?project卡住不動的解決
這篇文章主要介紹了vue創(chuàng)建項目卡住不動,vue?create?project卡住不動的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
使用vue實現(xiàn)簡單鍵盤的示例(支持移動端和pc端)
這篇文章主要介紹了使用vue實現(xiàn)簡單鍵盤的示例(支持移動端和pc端),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12

