TSone CLI

开发代理

开发服务器仅提供 HTTP。代理目标可使用 HTTP 或 HTTPS。规则按字面量路径前缀匹配,最长前缀优先。

import { defineConfig } from '@geektech/tsone-cli';

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

不需要其他选项时,代理也可以用字符串简写:

export default defineConfig({
  server: {
    proxy: {
      '/backend': 'http://localhost:4000',
    },
  },
});

行为

  • 代理保留查询字符串、请求方法、请求体与端到端头部,同时移除逐跳(hop-by-hop)头部。
  • changeOrigin: true 会改写上游 Host 头部;否则保留传入的 Host。
  • rewrite 在路径拼接到目标基础路径之前同步改写路径名。
  • 上游连接失败返回固定响应 502 Bad Gateway。

代理选项

ProxyOptions
名称签名说明
targetstring代理目标地址,HTTP 或 HTTPS。
changeOriginboolean改写上游 Host 头部为 target 的主机。
rewrite(path: string) => string在拼接目标基础路径前同步改写路径名。