角度代理配置不起作用 [英] angular proxy config not working

查看:430
本文介绍了角度代理配置不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白我哪里错了.

ps.已经尝试通过此答案解决问题,但仍无法正常工作

到后端的Angular-CLI代理不起作用

配置Angular-cli代理以进行自定义标头请求后端?

Angular-CLI代理不起作用

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' ]

package.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"
},

proxy.config.json

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

Angular CLI:6.1.2

节点:10.8.0

操作系统:darwin x64

角度:6.1.1

npm -v 6.2.0

解决方案

您仅匹配/api网址.如果要匹配以/api开头但还有其他内容的网址,请使用/api/*.

proxy.config.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/,请在proxy.config.json中包含pathRewrite:

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

i don't understand where i am wrong .

ps. already try to fix by this answer but still not working

Angular-CLI proxy to backend doesn't work

Configure Angular-cli proxy for custom headers in request to backend?

Angular-CLI proxy doesn't 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' ]

package.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"
},

proxy.config.json

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

Angular CLI: 6.1.2

Node: 10.8.0

OS: darwin x64

Angular: 6.1.1

npm -v 6.2.0

解决方案

You're matching only /api url. If you want to match urls that begin with /api but have something else use /api/*.

proxy.config.json

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

Explanation

This configuration will proxy from local-server (lets suppose it is localhost:4200) to your target, so:

  • localhost:4200/api --> http://localhost:1234/api
  • localhost:4200/api/product --> http://localhost:1234/api/product
  • localhost:4200 --> no proxy
  • localhost:4200/whatever --> no proxy

If you want to exclude /api/ from the target route, include pathRewrite inside in proxy.config.json:

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

这篇关于角度代理配置不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆