跳到主要内容

server

server.proxy


server.proxy 为开发服务器配置自定义代理规则。

语法

  • 字符串写法

    • 拼接路径:

      "/list": {
      target: "http://localhost:3002",
      changeOrigin: true,
      }
    • 重写路径:

      server: {
      proxy: {
      "/api": {
      target: "http://localhost:3002",
      changeOrigin: true,
      rewrite: (path) => path.replace(/^\/api/, '')
      }
      }
      }
  • 正则表达式写法: 如果key值以^开头,将会被解释为RegExp

    • 拼接路径:

      proxy: {
      "^/list/.*": {
      target: "http://localhost:3002",
      changeOrigin: true,
      }
      }
    • 重写路径:

      proxy: {
      "^/api/.*": {
      target: "http://localhost:3002",
      changeOrigin: true,
      rewrite: (path)=> path.replace(/^\/api/,"")
      }
      }