Redirect Angular2 http requests through proxy

后端 未结 1 1742
灰色年华
灰色年华 2021-01-14 19:48

I\'m creating Angular2 application using angular-cli bootstrapper and use webpack-dev-server for debugging purposes.

For some kinds of http request I wa

相关标签:
1条回答
  • 2021-01-14 20:13

    I found solution. I've created simple express server to log all requests transferred from proxy server to inspect how mapping works. And I found out that I had wrong understanding about mapping.

    Instead of strict mapping defined in config file:

    /api/users => http://localhost:4201/users
    

    webpack proxy server map using next rule:

    /api/users => http://localhost:4201/users/api/users
    

    So solution here is to add pathRewrite parameter to exclude prefix

    {
      "/api/users": {
        "target": "http://localhost:4201",
        "secure": false,
        "pathRewrite": {"^/api" : ""}
      }
    }
    
    0 讨论(0)
提交回复
热议问题