简体   繁体   中英

Need a common mongodb connection for every module in nodejs

I am working on a task with different modules.

I require a common mongodb connection for each and every module..

How can I write in some module and use in this because the db connection is also required in some other modules...

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
var dbo;
MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  dbo = db.db("mydb");
});

router.post('/', function(req, res) {

    dbo.collection("customers").find({"userid":req.body.userid}).toArray(function(err, result) {
    if (err) throw err;
    if(result.length>0){
        res.send("username already taken please enter differnt username ")
    }

    else if(req.body.fname==undefined||(!validator.isAlpha(req.body.fname))){
    res.send("please enter only alphabets as fname ")
   }

   else if(req.body.lname==undefined||(!validator.isAlpha(req.body.lname))){
    res.send("please enter only alphabets as lname ")
   }

   else if(req.body.userid==undefined||(!validator.isAlphanumeric(req.body.userid))){
    res.send("please enter only alphanemric as user name ")
   }

    else if(req.body.pwd==undefined||req.body.pwd.length<6){
    res.send("please enter atleast  6 charcaters as password ")
   }

   else{
            var bcrypt = require('bcryptjs');
            var salt = bcrypt.genSaltSync(10);
            var hash = bcrypt.hashSync(req.body.pwd, salt);
            req.body.pwd=hash;


        dbo.collection("customers").insertOne(req.body, function(err, res) {
        if (err) throw err;
        console.log("1 document inserted");
        });
        res.send(req.body);
    }

 });


    });



module.exports = router;

use can use node export and import, to that you can use mongodb connection instance in other modules also, assuming dbo is variable where you want to store mongodb connection

export let dbo;
MongoClient.connect(url, function(err, db) {
if (err) throw err;
dbo = db.db("mydb");
});

you can assign db connection to dbo variable and and use it in whichever module you want

you have to create a middleware for your connection and then assign the db object to your request object ( req in your case )

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

router.use( (req,res,next) => {
   MongoClient.connect(url, function(err, db) {
     if (err) throw err;
     req.dbo = db.db("mydb");
     next();
  });
})

in your router.post("/", ...) you will do req.dbo.collection(...)

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";
var dbo=null;

exports.conection=function(){
    if(dbo!=null) return 

  MongoClient.connect(url, function(err, db) {
  if (err) throw err;
   dbo = db.db("mydb");


});
}


exports.get = function (){
    return dbo;
}

i tried this and i use get method when ever i require this is working for me

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM