FANDOM



In order to use Mongoose you must establish a connection with your database.

Here is a boilerplate module I use to initialize the database.

var mongoose = require('mongoose');

module.exports = {
    _db: null,
    init: function(){
        if (!module.exports._db){
            var path = 'mongodb://' + SERVER_NAME + '/' + DB_NAME;
            console.log('connecting to MONGO via ' + path);
           module.exports._db = mongoose.connect(path);
        }
        return module.exports._db;        
    }
    
} 

The constants are set by global in a central bootstrapping function:

module.exports.init = function(app) {

    global.SERVER_NAME = 'localhost:27017';
    global.DB_NAME = 'glitterwood'; // obviously localized to my app - choose your own
    require('./../models/db').load();

You only have to init your database once on application startup.

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.