title: Setting up mongodb on Heroku with Node
body: <p>On my local host, I have the following Node code to setup a mongoDB database name "dbname":</p>
  
  <p><strong>users.js:</strong></p>
  
  <pre><code>var MongoClient = require("mongodb").MongoClient,
      Connection = require("mongodb").Connection,
      Server = require("mongodb").Server;
  
  Users = function(host, port) {
    var mongoClient = new MongoClient(new Server(host, port));
    mongoClient.open(function (){});
    this.db = mongoClient.db("dbname");
  };
  
  Users.prototype.getCollection = function (callback) {
    this.db.collection("users", function (error, users) {
      if (error) callback(error);
      else callback(null, users);
    });
  };
  
  Users.prototype.findAll = function (callback) {
    this.getCollection(function (error, users) {
      if (error) {
        callback(error);
      } else {
        users.find().toArray(function (error, results) {
          if (error) {
            callback(error);
          } else {
            callback(null,results);
          }
        });
      }
    });
  }
  
  // Bunch of other prototype functions...
  
  exports.Users = Users;
  </code></pre>
  
  <p>I like to put the above database functionality in one file, and then in my main server file require that file as follows:</p>
  
  <p><strong>server.js:</strong></p>
  
  <pre><code>var Users = require("./users").Users;
  var users = new Users("localhost", 27017);
  users.findAll(function (err, user) {
    // Do something
  });
  </code></pre>
  
  <p>To have this working on localhost is pretty easy. In the command line, I just type the following:</p>
  
  <pre><code>$ mongod # to launch the database server
  $ node server.js # to launch the web server
  </code></pre>
  
  <p>and it works fine. However, now I'm trying to push the whole thing onto Heroku with the mongolab addon</p>
  
  <pre><code>heroku addons:add mongolab
  </code></pre>
  
  <p>but the database is not running and I have no idea how to make it run. <a href="https://devcenter.heroku.com/articles/getting-started-with-nodejs#using-mongodb" rel="nofollow">This tutorial</a> explains how to setup mongodb with the mongolab URI, but that's not how my code works, I use a host and a port and I create a new server based on that. How should I change my code for it to work on the heroku app? I want to keep the database code in a separate file, with the prototype functions.</p>