vite 项目配置跨域代理

vite.config.ts 文件下添加如下配置

server: {
cors: true, // 默认启用并允许任何源
proxy: {
'/api': {
target: 'http://192.168.1.2:8001/',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, '')
},
}
}

文件全文:

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
server: {
cors: true, // 默认启用并允许任何源
proxy: {
'/api': {
target: 'http://192.168.1.2:8001/',
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, '')
},
}
}
})
Published At