Angular-CLI proxy to backend doesn't work

后端 未结 9 983
不知归路
不知归路 2020-11-28 08:00

https://github.com/angular/angular-cli#proxy-to-backend here is an instruction how to do proxying to backend. I did everything step by step and still requests aren\'t proxie

相关标签:
9条回答
  • 2020-11-28 08:21

    Could you try with this one:

    {
      "/api": {
        "target": "http://url.com",
        "secure": false,
        "pathRewrite": {"^/api" : ""}
      }
    }
    

    It works for me,

    ** NG Live Development Server is running on http://localhost:4200. **
     10% building modules 3/3 modules 0 active[HPM] Proxy created: /api  ->  http://ec2-xx-xx-xx-xx.ap-south-1.compute.amazonaws.com
    [HPM] Proxy rewrite rule created: "^/api" ~> ""
    
    0 讨论(0)
  • 2020-11-28 08:27

    This was close to working for me. Also had to add

    "changeOrigin": true,
    

    full proxy.conf.json shown below:

    {
      "/proxy/*": {
      "target": "https://url.com",
      "secure": false,
      "changeOrigin": true,
      "logLevel": "debug",
      "pathRewrite": {"^/proxy" : ""}
      }
    }
    
    0 讨论(0)
  • 2020-11-28 08:27

    On MAC this works for me

    Angular 4 running localhost: http://localhost:4200/

    In package.json

    "start": "ng serve --proxy-config proxy.config.json",
    

    In proxy.config.json

    Where our-company-server would be replaced by off-site URL

    {
      "/v1": {
        "target": "https://our-company-server.com:7002",
        "secure": false,
        "logLevel": "debug"
      }
    }
    

    Where an angular GET request would be...

    this.http.get('/v1/dashboard/client', options).map...
    
    // options are headers, params, etc...
    // then .map the observable in this case.
    
    0 讨论(0)
  • 2020-11-28 08:29

    I had to make a small adjustment based on the above answers, although it seems a bit odd looking at the config now.

    This is my proxy.conf.json shown below:

    {
      "/api/*": {
         "target": "https://url.com",
         "secure": false,
         "changeOrigin": true,
         "logLevel": "debug",
         "pathRewrite": {"^/api" : "http://url.com/api"}
      }
    }
    

    Basically, I rewrote the path completely. And it works now.

    0 讨论(0)
  • 2020-11-28 08:29

    For those having a custom localhost domain, refer to this solution

    {
      "/api/*": {
        "target": "http://backend.site.example",
        "secure": false,
        "changeOrigin": true,
        "pathRewrite": {
          "^/api": "http://backend.site.example/api"
        }
      }
    }
    
    0 讨论(0)
  • 2020-11-28 08:33

    Proxy attribute pathRewrite should be added in the proxy.conf.json. See the example below. { "/services/*": { "target": "http://yoururl.com", "secure": false, "changeOrigin" : true, "pathRewrite": { "^/services" : "" } } }

    and run ng serve --proxy-config proxy.conf.json Surely it will work.

    0 讨论(0)
提交回复
热议问题