角度代理配置不起作用

2024-05-15

我不明白我错在哪里。

附:已经尝试通过这个答案修复但仍然不起作用

Angular-CLI 代理到后端不起作用 https://stackoverflow.com/questions/39809008/angular-cli-proxy-to-backend-doesnt-work

为后端请求中的自定义标头配置 Angular-cli 代理? https://stackoverflow.com/questions/44872498/configure-angular-cli-proxy-for-custom-headers-in-request-to-backend

Angular-CLI 代理不起作用 https://stackoverflow.com/questions/43616755/angular-cli-proxy-doesnt-work

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

** Angular Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
 10% building modules 3/3 modules 0 active[HPM] Proxy created: /api  ->  
http://localhost:1234
[HPM] Subscribed to http-proxy events:  [ 'error', 'close' ]

包.json

{
  "name": "budget",
  "version": "0.0.0",
  "scripts": {
   "ng": "ng",
  "start": "ng serve --proxy-config proxy.config.json",
  "build": "ng build --prod",
  "test": "ng test",
  "lint": "ng lint",
  "e2e": "ng e2e"
},

代理配置.json

{
  "/api": {
  "target": "http://localhost:1234",
  "secure": false,
  "changeOrigin": true,
  "logLevel": "debug"
  }
}

角度 CLI:6.1.2

节点:10.8.0

操作系统:达尔文 x64

角度:6.1.1

npm -v 6.2.0


你只匹配/api网址。如果你想匹配以/api但还有别的用处/api/*.

代理配置.json

{
  "/api/*": {
    "target": "http://localhost:1234",
    "secure": false,
    "changeOrigin": true,
    "logLevel": "debug"
  }
}

解释

此配置将从本地服务器代理(假设它是localhost:4200)到你的目标,所以:

  • localhost:4200/api --> http://localhost:1234/api
  • localhost:4200/api/product --> http://localhost:1234/api/product
  • localhost:4200--> 没有代理
  • localhost:4200/whatever--> 没有代理

如果你想排除/api/从目标路线开始,包括pathRewrite里面在里面proxy.config.json:

{
  "/api/*": {
    "target": "http://localhost:1234",
    "pathRewrite": { "^/api": "" },
    "secure": false,
    "changeOrigin": true,
    "logLevel": "debug"
  }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

角度代理配置不起作用 的相关文章