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

Share this post:

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_APPLICATION environment 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()
var appEnv = cfenv.getAppEnv()


app.listen(appEnv.port, appEnv.bind,  function() {
  util.log("server starting on " + appEnv.url)

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.

Add Comment

Leave a Reply

Your email address will not be published.Required fields are marked *


Hi – I just finished working through the exercise you provided on bluemix-service-switcher.

Brilliant! Very helpful!

I am new to node so this may be a ‘newbie question’. How do I incorporate your code into apps I have developed so I can run local — then move it to BlueMix — as you demonstrated. Specifically, do I need to copy and paste your server.js code in as part of my server.js code — or is more required here to make this work?

Thanks much for any additional instruction you can provide!



PDHoward, it depends on what code you are trying to reuse.

If you just want to use the `cfenv` package that my sample uses, you would normally put that in a `package.json`, just like the sample project does (along with express).

The code in the actual application is in a single file – `server.js` – but it itself is not designed to be reusable, since … it’s a complete app. Just copy/paste the bits you need.

More How-tos stories

A predictive Machine Learning model from Build to Retrain

This post is an excerpt from our solution tutorial that walks you through the process of building a predictive machine learning model, deploying it as an API to be used in applications, testing the model and retraining the model with feedback data. All of this happening in an integrated and unified self-service experience on IBM Cloud.

Continue reading

IBM Announces Promotions for Cloud Servers and Components

IBM is excited to announce 50% off list price for 60 days on selected virtual server and bare metal server offerings. This is your chance to try out a wide variety of IBM Cloud infrastructure offerings – from our fast, flexible and easily provisioned virtual servers to our high performance, totally customizable bare metal servers running Intel®'s Xeon® Scalable processors – perfect for driving today's HPC and cognitive / analytics workloads to new performance levels.

Continue reading

IBM Cloud Container Service Live in São Paulo, Brazil

IBM Cloud Container Service is a managed Kubernetes service providing an intuitive user experience with on-going cluster management. Built-in security and isolation to enable rapid delivery of apps, while leveraging IBM Cloud Services including Weather data, IoT, Analytics, or AI capabilities with Watson. Available in six IBM regions WW, including 20+ datacenters.

Continue reading