Hi, Manish
 

> I am very much confuse how i can use require funcationality effectively to 
> pass object instance across project. one i can think of present scenario is 
> creating db connection instance  on the main server.js page where i am 
> creating http object and pass this object to function call.  
>
> Please advice me what are the best possible ways to achieve such 
> requirements. so that I will create main instance only once and keep on 
> using it for my every request come via node HTTP server. 
>
>  
There are projects like this one to show how to start:

https://github.com/madhums/node-express-mongoose-demo


OTOH, my project is about real case of using single db with
connection via `mongodb-native` without any additional libs,
but with robust setup (reconnection, etc.) and simple interface:

https://github.com/olecom/enjsms/blob/master/app_main/app_back.js
https://github.com/olecom/enjsms/blob/master/app_main/lib/mongodb.js
https://github.com/olecom/enjsms/blob/master/app_main/lib/app.js

In any general case access to the current db connection from
any node.js module is done via:

```
var db = require('./lib/mongodb.js').client
// but check manually if it is set up yet
```

In any application module(specific to that project), where `api` is passed,
db can be accessed like so [1]

```
function middleware(req, res, next){
var db, hst

    db = api.db
    hst = db.getCollection('hst')

    return hst.find({}).toArray(function(err, arr){
        if(err) return next(err)
        if(!arr.length) return next('apperror_zero')

        // now there is a valid array of data
        return res.json(arr)
    })
}
```

-- 
Job board: http://jobs.nodejs.org/
New group rules: 
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
--- 
You received this message because you are subscribed to the Google Groups 
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/nodejs/b4db08a3-8c2e-4835-8afd-9674173054a2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to