简体   繁体   English

Node.js中的每个模块都需要一个公共的mongodb连接

[英]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.. 对于每个模块,我都需要一个公共的mongodb连接。

How can I write in some module and use in this because the db connection is also required in some other modules... 我如何在某些模块中编写和使用它,因为在其他一些模块中也需要db连接...

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 use可以使用节点导出和导入,也可以在其他模块中使用mongodb连接实例,假设dbo是要存储mongodb连接的变量

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 您可以将db连接分配给dbo变量,并在所需的任何模块中使用它

you have to create a middleware for your connection and then assign the db object to your request object ( req in your case ) 您必须为连接创建一个中间件,然后将db对象分配给您的请求对象(在您的情况下为req

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(...) 在您的router.post("/", ...)您将执行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 我尝试过这个并且只要需要我就可以使用get方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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