javascriptjqueryibm-cloudcloud-foundryibm-cloud-plugin

How to read VCAP_SERVICES env variable in java script


I'm getting java script error - 'Uncaught ReferenceError: process is not defined' while trying to refer below code in my JavaScript.

if(process.env.VCAP_SERVICES){
}

Should I need to include or declare or define any thing before reading VCAP_SERVICES environment variable?

Plan is to read credentials out of it and invoke corresponding IBM Bluemix service. Can any one suggest me on priority please?


Solution

  • From your comments it seems you're trying to run this as client-side javascript (through the web browser).

    The VCAP_SERVICES variable is an environment variable for the host machine i.e. the server hosting your bluemix application

    To access and process this variable you need to write server-side code in something like node.js, java, python etc...

    It is possible to connect to a service from client-side javascript but it's very bad practice because the service credentials will have to be stored client-side. You also need to manually copy across the environment variables for your service (host, port, username, password etc..) which is also bad practice because these variables may well change over time.