DZone
Cloud Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Cloud Zone > Using Cfenv for Easier Node.js on Cloud Foundry

Using Cfenv for Easier Node.js on Cloud Foundry

You can use Node.js's cfenv middleware to help make working with Cloud Foundry a lot easier. Made much easier is parsing out values within JSON structures.

Lorna Mitchell user avatar by
Lorna Mitchell
·
Jan. 23, 17 · Cloud Zone · Tutorial
Like (1)
Save
Tweet
4.60K Views

Join the DZone community and get the full member experience.

Join For Free

Now that I'm working at IBM, I am making extensive use of Bluemix, which is based on Cloud Foundry. The way that Cloud Foundry is set up is actually very neat, with everything you need contained in JSON structures within environment variables. Parsing out those values can be a pain, however, so I thought I'd share the library that's helping me the most with this: cfenv.

Cloud Foundry Environment Variable Structure

In one of my apps, which has both a Cloudant (CouchDB) database, and a RabbitMQ queue, the configured environment looks something like this (use the cf env command to see the environment values available to your app):

{
 "VCAP_SERVICES": {
  "compose-for-rabbitmq": [
   {
    "credentials": {
     "ca_certificate_base64": "LS0tLS1...",
     "db_type": "rabbitmq",
     "deployment_id": "585164fad9718500180011b3",
     "name": "bmix_lon_yp_49c23a93_1583_4971_b0f1_5cd638c37a78",
     "uri": "amqps://admin:REDACTED@bluemix-sandbox-dal-9-portal.4.dblayer.com:20264/bmix_lon_yp_49c23a93_1583_4971_b0f1_5cd638c37a78",
     "uri_admin": "https://bluemix-sandbox-dal-9-portal4.dblayer.com:20264",
     "uri_admin_1": "https://bluemix-sandbox-dal-9-portal5.dblayer.com:20264",
     "uri_direct_1": "amqps://admin:REDACTED@bluemix-sandbox-dal-9-portal.5.dblayer.com:20264/bmix_lon_yp_49c23a93_1583_4971_b0f1_5cd638c37a78"
    },
    "label": "compose-for-rabbitmq",
    "name": "guestbook-messages",
    "plan": "Standard",
    "provider": null,
    "syslog_drain_url": null,
    "tags": [
     "big_data",
     "data_management",
     "ibm_created"
    ]
   }
  ]
 }
}


What the above is telling me is that inside a variable called VCAP_SERVICES, I'll find the following information relating to my RabbitMQ service that this app uses. It's entirely possible to parse the JSON and pull out the values the app needs, but it's clunky, especially with hyphenated key names! Instead, the cfenv library makes this super simple.

The code for this is on GitHub if you want to see it in context, but essentially, it boils down to:

Bring in the dependency:

var cfenv   = require('cfenv');


Next, parse the environment variable into something we can use:

    var appEnv = cfenv.getAppEnv()


Finally, go ahead and use those variables that are now in the appEnv (you might like to make this a constant rather than a variable since updating it will do nobody any good):

    rabbitmq_url = appEnv.getService('guestbook-messages').credentials.uri;


Hopefully, this shows how the cfenv library makes it easy to work with the Cloud Foundry environment variables and will remind me how to solve this the next time I run into problems parsing a complicated data structure! There are equivalents for other languages, too! For example, if you use PHP, try installing cf-helper-php via Composer.

Cloud Foundry Cloud Node.js

Published at DZone with permission of Lorna Mitchell, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Top ALM Tools and Solutions Providers
  • 3 Reasons Why You Should Centralize Developer Tools, Processes, and People
  • Component Testing of Frontends: Karate Netty Project
  • Data Mesh — Graduating Your Data to Next Level

Comments

Cloud Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo