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.