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

  • Home
  • SEARCH
  • 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 7980729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:09:55+00:00 2026-06-04T10:09:55+00:00

I am playing around with node.js and based on this wonderful tutorial I created

  • 0

I am playing around with node.js and based on this wonderful tutorial I created two providers (SchemaProvider and EntityProvider).

They look both like:

var Db = require('mongodb').Db;
var Connection = require('mongodb').Connection;
var Server = require('mongodb').Server;
var BSON = require('mongodb').BSON;
var ObjectID = require('mongodb').ObjectID;

EntityProvider = function(host, port) {
this.db = new Db('timerange', new Server(host, port, {auto_reconnect: true}, {}));
this.db.open(function() {
    console.log("Schema Provider has connected and may be used as of now.");
});
};

EntityProvider.prototype.getCollection = function(callback) {
this.db.collection('entity', function(error, collection) {
    if (error) {
        callback(error)
    } else {
        callback(null, collection);
    }

});
};

EntityProvider.prototype.findById = function(id /* The id to be found */, callback) {
this.getCollection(function(error, collection) {
    if (error) {
        callback(error);
    } else {
        collection.findOne({_id: id}, function(error, result) {
            if (error) {
                callback (error);
            } else {
                callback(null, result);
            }
        });
    }
});
};

In app.js I require(‘provider’) where both providers are defined.

Then I do:

schemaProvider = new SchemaProvider('192.168.0.50', 27017); 
entityProvider = new EntityProvider('192.168.0.50', 27017); 

Now, I created a module named dao (I come from a java/spring perspective :-)). As I didn’t use “var”, both variables and hence providers are accessible in my DAO. If I used “var”, the providers wouldn’t be accessible.

My question is :

If I wanted to use only one instance of the provider throughout the whole application, how would I do that?

Thanks in advance!

  • 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-04T10:09:57+00:00Added an answer on June 4, 2026 at 10:09 am

    Setting globals (not using var) is a really bad practice, you should always avoid that.

    If you want to have only an instance of the provider in your whole application you can do something like this:

    provider.js

    var providerInstance;
    
    // define provider here
    
    module.exports = function() {
      providerInstance = providerInstance || new Provider('192.168.0.50', 27017);
      return providerInstance;
    }
    

    That way the provider object gets created only once and then reused each time you require it:

    app.js

    var provider = require('./provider')();
    

    app2.js

    // using the same object as in app.js
    var provider = require('./provider')();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am playing around with Hadoop and have set up a two node cluster
I was playing around with node.js and something strange happens when you run this
After playing around with haskell a bit I stumbled over this function: Prelude Data.Maclaurin>
Been playing around with this for a couple of hours and can't seem to
I've been playing around with node.js (nodejs) for the past few day and it
While playing around with playframework yabe-siena-gae, I noticed a datastore file created for the
I'm playing around with Journey on node and I only realized that the router
I have been playing around with the youtube API and node.js, so far I
I'm playing around with node.js, trying to re-write a particularly poorly designed part of
So I am developing (more playing around with) a realtime game in node.js, I

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.