首页 > 技术文章 > React:通过vite构建时,解决跨域问题

testopsfeng 2022-02-27 21:30 原文

vite.config.ts 中添加

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

// https://vitejs.dev/config/
export default defineConfig({
  server: {
    proxy: {
      '/api': {
        target: 'http://ali.testops.top:9200',
        // target: 'http://localhost:9200',
        changeOrigin: true,
        rewrite: (path: string) => {
          // console.log('http://ali.testops.top:9199' + path.replace(/^\/api/, ''));
          return path.replace(/^\/api/, '')
        }
      }
    }
  },
  plugins: [reactRefresh()],
})**

推荐阅读