You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Angular 15代理路径能否使用正则?如何批量代理含test的子路径?

Angular 15 代理匹配含test子串路径的正确配置

你的代理配置不生效是因为直接把正则表达式作为路径键的写法不符合Angular代理的规则,Angular代理默认采用前缀匹配,要实现匹配所有包含test子串的路径,有两种可行方案:

方案一:通配符匹配(简单易用)

用通配符模式匹配任何包含test子串的路径,配置如下:

{
  "/**/*test*/**": {
    "target": "http://localhost:1845",
    "secure": false,
    "changeOrigin": true
  }
}
  • /**/*test*/** 会匹配所有路径中包含test的请求,不管test在路径的开头、中间还是结尾
  • 加上changeOrigin: true可以避免后端验证Origin导致的跨域问题

方案二:正则表达式匹配(精准控制)

如果需要更精准的正则匹配,要通过context字段定义规则数组:

{
  "context": ["/.*test.*/"],
  "target": "http://localhost:1845",
  "secure": false,
  "changeOrigin": true
}
  • /.*test.*/ 是正则表达式,匹配任意包含test子串的路径
  • context字段专门用来指定需要代理的路径规则,支持正则表达式

注意事项

配置完成后,启动项目时要指定代理配置文件:

ng serve --proxy-config proxy.conf.json

内容的提问来源于stack exchange,提问作者emka26

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 22:45:30