Customise ng serve to proxy calls to /api?

前端 未结 1 1831
-上瘾入骨i
-上瘾入骨i 2021-01-31 08:29

I created a new application with ng CLI, works like a charm: ng new babysteps cd babysteps ng serve ng serve uses webpack to assemble the app. To fully test it, I

相关标签:
1条回答
  • 2021-01-31 09:31

    You can indeed setup a proxy to backend with the angular cli, with the --proxy-config flag.

    Here is more or less a copy-paste from the documentation:

    Say we have a server running on http://localhost:3000/api and we want all calls to http://localhost:4200/api to go to that server.

    We create a file next to projects package.json called proxy.conf.json with the content

    {
        "/api":
        {
            "target": "http://localhost:3000",
            "secure": false
        } 
    }
    

    [...]

    and then we edit the package.json file's start script to be

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

    and run it with npm start

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