[英]How to access System Specific (Cloud Foundry) environment Variable Programmatically using Angular?
I have an angular app which is hosted on cloud.I'm Using Cloud foundry for deployment purpose.我有一个托管在云上的 angular 应用程序。我正在使用 Cloud Foundry 进行部署。 I need to access cloud foundry environment variables in my angular app at runtime without using any third party library like Angular Universal or any backend api.我需要在运行时访问我的 angular 应用程序中的 Cloud Foundry 环境变量,而不使用任何第三方库,如 Angular Universal 或任何后端 api。
I have tried several links ( how to make angular application read environment variables from private cloud foundry (Runtime) ) but neither it's working nor matching my parameters.我已经尝试了几个链接( 如何使 angular 应用程序从私有云代工厂(运行时)读取环境变量),但它既不工作也不匹配我的参数。 Need your Help.需要你的帮助。 Thanks in Advance.提前致谢。
The typical way to do this would be something like in the post you referenced, or to run a very basic REST API that you could query to fetch environment variables.执行此操作的典型方法类似于您引用的帖子,或者运行非常基本的 REST API ,您可以查询以获取环境变量。 This is because the environment variables live in the container where your app runs on Cloud Foundry & your Javascript code executes on a completely different computer.这是因为环境变量存在于您的应用在 Cloud Foundry 上运行的容器中,并且您的 Javascript 代码在完全不同的计算机上执行。 The Javascript just has no access to that code. Javascript 无法访问该代码。
That said, you could make a really hacky version of server-side rendering by using the .profile
script.也就是说,您可以使用.profile
脚本制作一个非常 hacky 版本的服务器端渲染。 The trick is that CF allows you to include a .profile
script with your application.诀窍是 CF 允许您在应用程序中包含一个.profile
脚本。 This script is executed once prior to your application starting and it can modify your application files.此脚本在您的应用程序启动之前执行一次,它可以修改您的应用程序文件。 Thus, the script just needs to inject the environment variables somewhere into your application code.因此,脚本只需要将环境变量注入应用程序代码的某个位置。
There's a number of ways you could do this:有多种方法可以做到这一点:
.profile
script in Python/Ruby/Perl as those are present in the CF Linux stack by default.您还可以在 Python/Ruby/Perl 中编写.profile
脚本,因为它们默认存在于 CF Linux 堆栈中。 You just need to include the #!/usr/bin/env python
or whatever you need at the top of the .profile
file.您只需在.profile
文件的顶部包含#!/usr/bin/env python
或您需要的任何内容。Whatever solution you end up with, you should think about what information is present in the env variables and if that is leaking anything sensitive because if you inject the values into your Javascript, it will be visible to your users (ie view source/browser dev tools).无论您最终采用哪种解决方案,您都应该考虑环境变量中存在哪些信息,以及是否泄漏任何敏感信息,因为如果您将这些值注入 Javascript,您的用户将可以看到它(即查看源代码/浏览器开发工具)。
IMHO, it would probably be best to not inject the full set of env variables & skip things like VCAP_SERVICES
and VCAP_APPLICATION
which will almost certainly have sensitive information.恕我直言,最好不要注入全套环境变量并跳过像VCAP_SERVICES
和VCAP_APPLICATION
这样几乎肯定会有敏感信息的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.