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

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

Keeping up-to-date with Kubernetes

Kubernetes development and adoption continues to grow at a rapid pace, and keeping current can be difficult without the right process and tools. For example, IBM Cloud Container Service launched with support for Kubernetes v1.5.6 earlier this year. Since that initial launch, the Kubernetes community provided 3 minor releases (v1.6, v1.7, and v1.8) and over 25 patch releases. By year's end there's likely to be another minor release and numerous patches. So with all this change, what's the best way to keep your cluster and apps up-to-date and secure?

Continue reading

Setting Access Control Policies for IBM Cloud Object Storage

As your organization explores more digital initiatives, including cloud and mobile, the importance of identity and access management (IAM) is paramount. Nearly all IT decision makers we talk with agree that IAM is essential to the success of their company’s cloud adoption and it is seen as a key enabler for mobility, analytics and IoT initiatives.

Continue reading

Cloud Foundry Day – London England, Nov 29th

Come join us for a "Cloud Foundry Day", a free educational conference and networking event with talks by Cloud Foundry community members, industry leaders, and Cloud Foundry Developers. Hosted by Dr. Julian Friedman, IBM Open Source Development Lead, code contributor and a leader in the Cloud Foundry community, and joined by renown speakers in the Cloud Foundry community. Enjoy some great topics, good conversations, food and beer!

Continue reading