Using Environment Variables in Node.js Apps

You can use environment variables to inject new values into your app, so you don't have to redeploy the app when you need to change configuration items. Variables can then be changed from the App Details page on the Environment Controls tab, in the Application variables and Engine variables sub-tabs, or with the TIBCO Cloud™ - Command Line Interface.

To use environment variables in Node.js apps, you must first make changes to the app manifest file. This is done through a tool called tibcli-node, which is an open-source Node.js module available on npmjs.org. The tool can be used to update the manifest.json file with the new environment variables being inserted, before the app is pushed to TIBCO Cloud™ Integration.

To install and use tibcli-node:

  1. From any directory, install the npm package globally:
     %npm install -g tibcli-node
    
    This downloads the package from npmjs.org and installs it to your system globally. On Unix/Linux/Mac it is normally installed under the /usr/local/bin directory. Make sure this path is included in your shell's PATH environment variable.
  2. As an example, use a string type environment variable DB_USER in your Node.js code. You need to retrieve the value of this variable in your Node.js code as follows:
     process.env.DB_USER
    
    For example, you can write this code in the server.js file as follows:
     Logger.log(Logger.LOG_INFO, process.env.DB_USER);
    
  3. Browse to the directory containing your Node.js app's manifest file, and execute the following command from the command line:
     $ tibcli-node env addvar --varname DB_USER --type string --value "defaultvalue"
    
    This inserts a variable named DB_USER with type string and value "defaultvalue" to the manifest file. The variable types supported here are the JSON data types.
  4. Push the Node.js app as you normally do, such as with tibcli app push.
  5. After the app is pushed successfully, check the page App Details > Environment Controls > Application variables. The environment variable DB_USER is there, with an empty value specified. You can specify any value you want to pass in to the Node.js code, and then update the app.
  6. After the app is updated and running, check the logs. You can see the value you specify for this variable being printed out.

External Links

Node.js Tools for TIBCO Cloud™ Integration

Related Topics

Configuring App Variables

Configuring App Variables with the TIBCO Cloud™ - Command Line Interface

App Details Page