Angular CLI: Change REST API URL on build

后端 未结 7 1662
心在旅途
心在旅途 2021-02-02 08:58

I want to remove my local server prefix from my REST API URLs (example, http://localhost:8080) when building for production (ng build --prod).

I get that i

7条回答
  •  一向
    一向 (楼主)
    2021-02-02 09:41

    DEV/PROD parity approach

    One way to this approach using containers is by parsing your environment files before you launch your webserver. This assumes that your are following a good practice, that is, you aren't serving your angular application via ng.

    For example, a rudimentary approach would be to craft your index.html with your desired configuration, this is just an example, do it how you see best:

    
    

    Before launching your webserver with your static content, use a script that verifies and matches valid environment variables from your configuration window.config with your actual environment variables.

    This is doable with sed for instance, or if you want to go pro, you may also use [jq][1].

    After your validation is done, your index.html has the window.config object configured following the dev-prod parity approach from 12factors.

    Running your docker container would be just,

    docker run -it -e ENV_VAR1="my development value" ...

    and for production,

    docker run -it -e ENV_VAR1="my production value" ...

提交回复
热议问题