Using the node.js cfenv package to make your life easier

5 min read

Using the node.js cfenv package to make your life easier

I’ve just put together a little Bluemix application for node.js – bluemix-service-switcher – which shows how to access service information using the cfenv package.

This sample, and the cfenv package, should also work fine on any Cloud Foundry-based PaaS.

cfenv provides a number of functions to deal with the VCAP_SERVICES and VCAP_APPLICATIONenvironment variables.

You will never have to JSON.parse() them again!

Almost every server I write uses the following little snippet to start the server:

<code>var app    = express()<br>
var appEnv = cfenv.getAppEnv()<p></p>
<p>app.listen(appEnv.port, appEnv.bind,  function() {<br>
  util.log("server starting on " + appEnv.url)<br>

This will start your server on the right port, bind to right address, and print the url of the server when it’s started.

cfenv also works when you run locally, on your development machine, and provides appropriate default values when running in that environment.

The service-switcher application makes use of cfenv‘s functions to show how easy it is to access your bound services, and for your app be flexible about the services it binds to.

Be the first to hear about news, product updates, and innovation from IBM Cloud