angular proxy config not working

后端 未结 2 1378
眼角桃花
眼角桃花 2021-01-16 21:16

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

Con

相关标签:
2条回答
  • 2021-01-16 21:26

    change

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

    to

    {
      "/api": {
      "target": "http://localhost:1234",
      "secure": false,
      "changeOrigin": true,
      "logLevel": "debug",
    "pathRewrite": {
          "^/api": ""
        }
      }
    }
    
    0 讨论(0)
  • 2021-01-16 21:27

    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"
      }
    }
    
    0 讨论(0)
提交回复
热议问题