vite proxy 配置

Vite proxy 配置

简单记录一下通过设置 vite proxy 解决前端开发过程中请求跨域问题

配置

// vite.config.ts
import { defineConfig } from 'vite'

export default defineConfig({
  server: {
    proxy: {
      '/api': {
        target: 'http://localhost:3000',
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/api/, '')
      }
    }
  }
})

开发过程中前端请求的地址为 /api/interface,经过代理后,请求地址会变为 http://localhost:3000/interface

注意

评论

评论加载中…

输入关键词开始搜索。

选择Enter 打开Esc 关闭CtrlK 唤起