Community

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)
})
</code>

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
2 Comments

Leave a Reply to PDHoward Cancel reply

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


PDHoward

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!

Reply

pmuellr_ibm

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.

Reply
More How-tos Stories

Quickly scaffold a native mobile app with Push and Analytics

As mobile app developers, few of the many priorities in our bucket are engaging the users through Push Notifications and monitor the app usage through Mobile Analytics.

Continue reading

Db2 on Cloud offsite disaster recovery node is now in closed beta

Today, Db2 on Cloud already has excellent availability characteristics, with a 99.99% SLA and the ability to scale your database without app downtime, unlike other competitors in the market.

Continue reading

Utilizing Multi-Container Functionality with IBM Cloud Developer Tools CLI

The latest update to the IBM Cloud Developer Tools CLI, version 1.1.0, provides support for applications that use multiple containers which are configured using Docker Compose yaml files. These files contain necessary information about the containers you wish to run. With Docker installed, you already have the requisite dependency to run multiple containers locally.

Continue reading