Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8760955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:10:28+00:00 2026-06-13T15:10:28+00:00

I am looking for some direction here from some seasoned node.js programmers. The biggest

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T15:10:29+00:00Added an answer on June 13, 2026 at 3:10 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking for some direction on a script that I'd like to build. Essentially,
I'm just looking for some direction when it comes to NHibernate and WCF. At
[NOTE:I am really looking for some good debugging techniques here. Perhaps some tricks or
Hi just looking for some direction, I have a HTML form which has several
I'm looking for some advice as to which direction to head in re the
I am currently looking at some code for PHP detection of mobiles, which is
I'm fiddling with a bit of c++/opencv. I was looking some of the samples
Looking at some assembly code for x86_64 on my Mac, I see the following
Looking at some of the code System.Linq I've come across some examples of Buffer<TSource>
Looking for some advice on the best way to implement localization along with client

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.