.env文件的配置
【前端配置篇】vue项目之.env系列文件配置详解:.env文件配置全局环境变量-腾讯云开发者社区-腾讯云
看完上面的文章后,看下下面关于开发环境中的proxy的配置
对象中key值要做变量要用 [ ] 来包裹
// vite.config.js
import {defineConfig
} from 'vite'export default defineConfig({server: {proxy: {// 简单的路径前缀代理[process.env.VUE_APP_API_BASE_URL]: {target: `${process.env.VUE_APP_API_BASE_URL}${Number(process.env.VUE_APP_API_PORT)})}`, // 目标服务器地址changeOrigin: true, // 改变请求源头rewrite: (path) => path.replace(/^\/api/, '') // 重写路径},// 使用正则表达式的代理'^/socket.io': {target: 'ws://localhost:3000', // WebSocket目标服务器ws: true, // 启用WebSocket代理changeOrigin: true},// 带自定义选项的代理'/graphql': {target: 'http://api.example.com',changeOrigin: true,configure: (proxy, options) => {// 自定义代理实例配置proxy.on('error', (err, req, res) => {console.log('Proxy error:', err)})}},// 跳过特定条件的请求'/static': {target: 'http://cdn.example.com',bypass: (req) => {if (req.headers.accept ? .includes('html')) {console.log('Skipping proxy for HTML request')return req.path}}}}}
})