React:通过vite构建时,解决跨域问题

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()],
})**
posted @ 2022-02-27 21:30  Feng1024  阅读(845)  评论(0编辑  收藏  举报