Quasar CLI with Vite - @quasar/app-vite
當把 Quasar CLI 建立的專案資料夾與現有的後端整合時,常見的需求是在使用開發伺服器時存取後端 API。為了達成這個目的,我們可以並排 (或遠端) 執行開發伺服器和 API 後端,並讓開發伺服器將所有 API 請求代理到實際的後端。
如果您在 API 請求中存取相對路徑,這會很有用。顯然地,這些相對路徑在您開發時可能無法運作。為了建立一個類似於您已部署的網站/應用程式所使用的環境,您可以代理您的 API 請求。
若要配置代理規則,請編輯 /quasar.config
檔案中的 devServer.proxy
。底層使用了 http-proxy
。完整選項列表請參閱此處。
devServer: {
proxy: {
// string shorthand
'/foo': 'https://127.0.0.1:4567',
// with options
'/api': {
target: 'http://jsonplaceholder.typicode.com',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, '')
},
// with RegEx
'^/fallback/.*': {
target: 'http://jsonplaceholder.typicode.com',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/fallback/, '')
},
// Using the proxy instance
'/api': {
target: 'http://jsonplaceholder.typicode.com',
changeOrigin: true,
configure: (proxy, options) => {
// proxy will be an instance of 'http-proxy'
}
},
// Proxying websockets or socket.io
'/socket.io': {
target: 'ws://127.0.0.1:3000',
ws: true
}
}
}
content_paste