Reputation: 19970
In My app.js i have this piece of code.
app.use(function(req,res,next){
req.db = db;
next();
});
So basically attaching the db in every request.
I would like to know the best way to access db from other js files If any one can provide some samples, it will be great help for me
Upvotes: 0
Views: 372
Reputation: 1276
Node Modules are cached, thus if you made a module with:
db.js
//...
module.exports=monk('localhost:27017/table');
and when you need it in another file, you can just call:
var db=require('db.js');
Only the first time you call it, the monk
function will be run. The object will be cached, and the same object will be returned in any subsequent require
.
Upvotes: 1