Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js passing variables

I am looking for some direction here from some seasoned node.js programmers. The biggest issue I am running into is passing around variables into separate modules. For example in my server.js I have the following:

var db = mongoose.createConnection('localhost','test');

Now I am not running my routes inside of the server.js file they are separated into there own files. So for a blog example it might be like this:

app.get('/blog/post/:id',function(req,res){
    //do something here
}

Now this is where the problem comes in. I do not want to have to setup a database connection in each of my routes and not to mention I would think that it would make a ton of connections. How do I handle this, is there a sample "REAL WORLD" application out there because I cannot seem to find anything about this and I know people have had to have this problem before. I know that node caches the modules but I cant imagine that it would cache the connection given it was in its own module. I created a config module that just holds the site config so requiring this where I need it is not a problem. I imagine there are other things that I am gonna wanna do this with so it would be best to figure this out now.

Any help is appreciated.

like image 926
ngreenwood6 Avatar asked Oct 26 '12 04:10

ngreenwood6


2 Answers

Here's what I do in my real world app.

I have a module named redis (that's the database I'm using). It contains the following code:

var store;

exports.store = store = redis.createClient(config.port, config.url);

So, I can directly access the client, if I need to. I almost never do that. The same module contains code like this:

exports.getData = function(dataID, callback){

    var key = DATA_STORE_PREFIX;

    try{
        store.hget(key, dataID, callback);
    } catch(err){
        callback(err);
    }
}

I use this by including the redis module in one or more route modules and calling it like this:

var db = require('redis');

db.getData('someData', function(err, result){
    console.log(result); // real world code goes here!
});

The node module system takes care of the rest.

like image 93
Waylon Flinn Avatar answered Oct 05 '22 04:10

Waylon Flinn


One way to do it is to add the connection as a property of an object which will be available to all of the modules that need it. For example in my express app I have something like this in my main app file:

require('./config/database')(app);

and the config/database file looks like:

var mongoose = require('mongoose');

module.exports = function(app) {
  var database = app.get('env');
  var uri = database === 'production' ? 'something...' : 'localhost';
  return app.locals.db = mongoose.createConnection(uri, database);
};

Any modules that need a db connection can then access app.locals.db simply by exporting a function which takes app as an argument (just like above). Of course you'll need to modify this somewhat if you are using something other than express, but the idea remains the same.

like image 20
David Weldon Avatar answered Oct 05 '22 05:10

David Weldon