Node.js mongodb-native driver authentication in cloudfoundry

I’m currently trying to mess around with Node and Mongo for a bit of self learning. Ive been looking at various blog entries and have been messing with getting a simple blog written in node with a mongo db working.

Anyway I’m having trouble making it work in cloudfoundry. I’ve looked on the mongodb-native usergroup about authentication and found some node script:

  • Get Public IP Address for node.js application
  • Is there an equivalent of Node.js for .NET developers?
  • persistent sessions with passport, mongodb and express
  • is ENOENT from fs.createReadStream uncatchable?
  • Custom Error message using passport Bearer
  • require('babel/register') doesn't work
  • var Db = require('mongodb').Db;
    var Connection = require('mongodb').Connection;
    var Server = require('mongodb').Server;
    var BSON = require('mongodb').BSON;
    var ObjectID = require('mongodb').ObjectID;
    ArticleProvider = function(dbname,host, port,username,password) {
      this.db= new Db(dbname, new Server(host, port, {auto_reconnect: true}, {}));{
            this.db.authenticate(username, password, function(err, db) { 
                    callback(err, db); 

    I can’t for the life of me get this working.

           this.db.authenticate(username, password, function() {}); 
    TypeError: Cannot call method 'authenticate' of undefined
        at /home/ben/NodeJS/sandbox/NodeBlog/articleprovider-mongodb.js:10:17
        at /home/ben/NodeJS/sandbox/NodeBlog/node_modules/mongodb/lib/mongodb/db.js:81:14
        at /home/ben/NodeJS/sandbox/NodeBlog/node_modules/mongodb/lib/mongodb/connections/server.js:76:11
        at /home/ben/NodeJS/sandbox/NodeBlog/node_modules/mongodb/lib/mongodb/admin.js:16:12
        at [object Object].<anonymous> (/home/ben/NodeJS/sandbox/NodeBlog/node_modules/mongodb/lib/mongodb/admin.js:124:12)
        at [object Object].emit (events.js:67:17)
        at [object Object].<anonymous> (/home/ben/NodeJS/sandbox/NodeBlog/node_modules/mongodb/lib/mongodb/connections/server.js:97:12)
        at [object Object].emit (events.js:64:17)
        at Socket.<anonymous> (/home/ben/NodeJS/sandbox/NodeBlog/node_modules/mongodb/lib/mongodb/connection.js:108:16)
        at Socket.emit (events.js:64:17)

    Anyone able to point out where I’m being a complete facepalmer would be much appreciated.

  • Babel trying to load LESS style with webpack
  • Unable to verify the first certificate - ionic app upload error
  • npm start - using CORS
  • Express 4.0: Cannot find module 'html' & Cannot find module 'handlebars'
  • node.js bodyParser error
  • How do I make this JS function asynchronous?
  • 2 Solutions collect form web for “Node.js mongodb-native driver authentication in cloudfoundry”

    I have also faced same kind of problem. Trying harder I have found the solutions.

    This may be very helpful to all the mongodb developers.

    var ArticleProvider =  function(host, port, username, password) {
      this.db= new Db('my_db', new Server(host, port, {auto_reconnect: true}, {}));,data){
            data.authenticate(username, password,function(err2,data2){
                     console.log("Database opened");


    The easiest way connect this is to use a mongourl (mongodb://localhost:27017/db…) and the connect function in the node-mongodb-native drivers.

    Cloudfoundry starts a very specific authenticated database and then tells you about connecting to that database within the environment variables, which can make it tougher to debug. Ideally you want it to work on both your local machine and on Cloudfoundry in basically the same way.

    Fortunately, I posted an article on that walks you through a simple Cloudfoundry setup. It has step-by-step instructions from zero to up and running both locally and on Cloudfoundry.

    Plus there’s a github code sample.

    Node.js is the Best Javascript runtime in the world.