No Batteries Required - Home
Node to Self: Get Ready for Asynchronous Programming - Part IIII
Ray Kahn
MAY 28, 2015 13:44 PM
A+ A A-

Connecting to mongoDB

As I mentioned in my last blog connecting to mongoDB is quite simple. However, as I use mongoose, you would first need to install the package using npm. 

What is Mongoose

Mongoose "provides a straight-forward, schema-based solution to modeling your application data and includes built-in type casting, validation, query building, business logic hooks and more, out of the box." Mongoose provides an object mapping interface within Node.js akin to translating data into JavaScript objects. For more information please visit their site.

Installing Mongoose

That's the easy part: npm install -g mongoose (you may omit the -g flag if you don't want a global install).

Configuring Node.js

I have placed the following in my Node.js server.js file:

* Module dependencies.
var config = require('./config.js');
var ImageUtil     = require('./util/ImageUtil');
var express = require('express');
var parseUrl      = require('url').parse;
var routes = require('./routes');
var mongoose = require('mongoose');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var http  = require('http');
var https         = require('https');
var path  = require('path');
var join          = require('path').join;
var appDir        = path.dirname(require.main.filename);
var util  = require('util');
var flash = require('connect-flash');
var fs            = require('fs');
var MongoStore    = require('connect-mongo')(express);
var apnagent      = require('apnagent');
var feedback      = new apnagent.MockFeedback();
var privateKey, certificate, certauth, sessionDB, sessionHost, sessionPort, sessionAutoReconnect;
var app           = module.exports = express();
// mongoDB connect
// if(NODE_ENV == 'development') 
app.configure('development', function(){
  console.log("Connecting to " + config.db_development.mongodb);
  privateKey  = fs.readFileSync('certs/ssl/private-key.pem', 'utf8');
  certificate = fs.readFileSync('certs/ssl/', 'utf8');
  certauth    = fs.readFileSync('certs/ssl/40ba1b1362481.crt', 'utf8');
  sessionDB             =; 
  sessionHost           =; 
  sessionPort           = config.session_development.port; 
  sessionAutoReconnect  = config.session_development.auto_reconnect; 
// if(NODE_ENV == 'production') 
app.configure('production', function(){
  console.log("Connecting to " + config.db_production.mongodb);
  privateKey  = fs.readFileSync(appDir + '/certs/ssl/private-key.pem', 'utf8');
  certificate = fs.readFileSync(appDir + '/certs/ssl/', 'utf8');
  certauth    = fs.readFileSync(appDir + '/certs/ssl/40ba1b1362481.crt', 'utf8');
  sessionDB             =; 
  sessionHost           =; 
  sessionPort           = config.session_production.port; 
  sessionAutoReconnect  = config.session_production.auto_reconnect; 
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
var credentials = {key: privateKey, cert: certificate, ca: certauth};
db.once('open', function callback () {

// instantiate your controllers here
http.createServer(app).listen(3000, function(){
console.log('Express server listening on port 3000');
What's Next
Next time I will share with you my flow chart and how I have set-up my environment. Things are just getting interesting.
[%= name %]
[%= createDate %]
[%= comment %]
Share this:
Please login to enter a comment: