简体   繁体   中英

How to pass backend api url from command line in my angular 2 app?

I want to deploy my angular app to multiple amazon instances (more than 10). For each instance the backend api also changes.

I dont want to hardcode the api url in environment files and change the environment like below

ng serve --environment=prod

I want to specify the backend url with ng serve like below

ng serve --backendApiUrl=http://www.example.com/api.

is that possible?

Not possible out of the box.

The first option would be to put your backend api url behind a load balancer, so if there is a change the config needs to updated in one plus; plus you get the added benefit of load balancing. Something like AWS Elastic Load Balancing or Netflix could help.

Assuming you can't do that, the ng serve command doesn't have the ability to do that ( ng serve source ). So your best options would be to write a bash script that would take that as an argument, and store that for ng2 to use. Obviously at this point your angular code is already bundled together, so there won't be a way to inject/weave into your code as you would be able to at build time with webpack, etc. But you could achieve this with a separate file that angular pulls into define the backend API. Then your bash script would only need to overwrite this file to update the URL.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM