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
:
-
From any directory, install the npm package globally:
%npm install -g tibcli-node
This downloads the package fromnpmjs.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'sPATH
environment variable. -
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 theserver.js
file as follows:Logger.log(Logger.LOG_INFO, process.env.DB_USER);
-
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 namedDB_USER
with typestring
and value"defaultvalue"
to the manifest file. The variable types supported here are the JSON data types. -
Push the Node.js app as you normally do, such as with
tibcli app push
. -
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. - 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 with the TIBCO Cloud™ - Command Line Interface