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. 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. The Javascript just has no access to that code.
That said, you could make a really hacky version of server-side rendering by using the .profile
script. The trick is that CF allows you to include a .profile
script with your application. 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:
- There are some pure Bash solutions here and here.
- You can also write the
.profile
script in Python/Ruby/Perl as those are present in the CF Linux stack by default. You just need to include the #!/usr/bin/env python
or whatever you need at the top of the .profile
file.
- You could include a static binary (Go, Rust, etc..) and have that process your files.
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 (i.e. view source/browser dev tools).
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.