2

I have multiple routes that need to access a database, for development I use a local database, and obviously production I use a hosted database

The only problem is every time I go to push a release I have to go through each route manually changing the database link

e.g.

var mongodb = require('mongojs').connect('urlhere', ['Collection']);

It would be nice if I could declare a variable in app.js like

app.set('mongoDBAddress', 'urlhere');

then in each file do something like var mongodb = require('mongojs').connect(app.get('mongoDBAddress'), ['Collection']);

Does anybody know if this is achievable I've been messing around with it for about an hour googling and trying to include different things but I have no luck. thanks.

Datsik
  • 14,453
  • 14
  • 80
  • 121

2 Answers2

2

From the docs:

In browsers, the top-level scope is the global scope. That means that in browsers if you're in the global scope var something will define a global variable. In Node this is different. The top-level scope is not the global scope; var something inside a Node module will be local to that module.

You have to think a bit differently. Instead of creating a global object, create your modules so they take an app instance, for example:

// add.js
module.exports = function(app) { // requires an `app`
  return function add(x, y) { // the actual function to export
    app.log(x + y) // use dependency
  }
}

// index.js
var app = {log: console.log.bind(console)}
var add = require('./add.js')(app) // pass `app` as a dependency

add(1, 2)
//^ will log `3` to the console

This is the convention in Express, and other libraries. app is in your main file (ie. index.js), and the modules you require have an app parameter.

You can add a global variable to GLOBAL, see this this question, although this is probably considered bad practice.

Community
  • 1
  • 1
elclanrs
  • 92,861
  • 21
  • 134
  • 171
  • 1
    No. There is a global scope, only it's not the module's toplevel scope. Your first sentence sounds as if every module had its own *global* scope, which is not true. – Bergi Jul 12 '14 at 22:35
  • @Bergi: yeah... I'll just leave the quote from the documentation. – elclanrs Jul 12 '14 at 22:38
0

We have two methods in node.js to share variables within modules.

  1. global
  2. module.export

But your problem seems to be different, what I got is you want to connect your application to different databases without changing code. What you need to do is use command line params

For more ref

server.js

var connectTo = {
    dev : "url1"
    production : "url2"
}

var mongodb = require('mongojs').connect(connectTo[process.argv[2]], ['Collection']);

Run your server.js as

node server.js dev
// for connecting to development database

or

node server.js production
// for connecting to prodiction database

To share connection across diffrent modules

//Method 1
global.mongodb = require('mongojs').connect(connectTo[process.argv[2]], ['Collection']);

//Method 2
exports.mongodb = require('mongojs').connect(connectTo[process.argv[2]], ['Collection']);

exports.getMongoDBAddress = function() {
    return connectTo[process.argv[2]]
}
Community
  • 1
  • 1
Harpreet Singh
  • 2,651
  • 21
  • 31