server跨域代理
在vite.config.ts 文件中直接配置
export default defineConfig({
//其他配置...
//设置反向代理,跨域
server: {
// host: '0.0.0.0',//设置地址:http://localhost
port: 4500, // 设置服务启动端口号
// open: true, // 设置服务启动时是否自动打开浏览器
// cors: true, // 允许跨域
proxy: {
'/api': {
target: 'http://localhost:3001/',//代理的地址
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, '')//这里的/需要转义
}
}
},
})