[英]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). 我想将我的角度应用程序部署到多个亚马逊实例(超过10个)。 For each instance the backend api also changes.
对于每个实例,后端api也会更改。
I dont want to hardcode the api url in environment files and change the environment like below 我不想在环境文件中对api url进行硬编码并更改如下所示的环境
ng serve --environment=prod
I want to specify the backend url with ng serve like below 我想用ng serve指定后端网址,如下所示
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; 第一种选择是将您的后端api url放在负载均衡器之后,因此,如果有更改,则需要在一个以上的时间内更新配置; plus you get the added benefit of load balancing.
另外,您还将获得负载平衡的额外好处。 Something like AWS Elastic Load Balancing or Netflix could help.
诸如AWS Elastic Load Balancing或Netflix之类的功能可能会有所帮助。
Assuming you can't do that, the ng serve
command doesn't have the ability to do that ( ng serve
source ). 假设您无法执行此操作,那么
ng serve
命令将无法执行此操作( 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. 因此,最好的选择是编写一个bash脚本,将其作为参数,并将其存储以供ng2使用。 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.
显然,此时您的角度代码已经捆绑在一起,因此将无法像在webpack等构建时那样将代码注入/编织到您的代码中。但是您可以使用单独的文件来实现角度拉入定义了后端API。 Then your bash script would only need to overwrite this file to update the URL.
然后,您的bash脚本只需要覆盖此文件即可更新URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.