Node.js helper module for IBM BlueMix


Node.js helper module for IBM BlueMix.

This module automates some common intialization tasks for Node.js applications running on IBM BlueMix. The main features of the module are:

  • parsing of common configuration enviroment variales (host, port, temporary directory)
  • parsing of VCAP services
  • search of VCAP services by name and pattern
  • automatic initialization of common services
npm install bluemix

The following sample code initializes an Express.js application and connects it to mongodb:

var bluemix = require('bluemix')
  , express = require('express')
  , app = express()
bluemix.on('ready', function() {
  console.log('App started on port ' + bluemix.config.port + ' and connected to mongodb');
  bluemix.db.collection('test').find().toArray(function(errdata) {
    if (err) {
      // handle error 
    } else {
      // work with data 
bluemix.on('error', function(err) {
  console.error('App init failed', err);

The object bluemix.config is automatically initilized at startup and contains the following attributes:

  • host, hostname/address of the application
  • port, port of the application
  • tmpDir, temporary directory of the application

Returns a list of services matching name, which can be a string or a RegExp. Example:

// get an array of available mongodb-2.2 services 
var a1 = bluemix.getServices('mongodb-2.2');
// get an array of mysql services, regardless of the version 
var a2 = bluemix.getServices(/mysql/);

Returns the first service matching name, which can be a string or a RegExp. Example:

// get the first available mongodb-2.2 service 
var s1 = bluemix.getService('mongodb-2.2');
// get the first mysql service, regardless of the version 
var s2 = bluemix.getService(/mysql/);

Connects to all the available (and supported) services. The method emits a ready event upon successful connection all the services and if all the dependencies (if any) are satisfied. It emits an error event otherwise.

The following table lists the supported services, which drivers (npm modules) are used to connect and how to access them:

ServiceService nameDriverConnected client instance
MongoDBmongodb-2.2mongodbbluemix.db, bluemix.mongodb, bluemix.rabbitmq
Mysqlmysql-5.5mysqlbluemix.db, bluemix.mysql

If both MongoDB and Mysql are defined, bluemix.db will point to the first one that was encountered. The same applies if multiple MongoDB (or Mysql) services are defined. Services can also be accessed as bluemix[service type][service name]: