Connecting to MongoDB over SSL with Node.js

前端 未结 3 1827
一整个雨季
一整个雨季 2020-12-14 23:11

How do I connect to a MongoDB-server over SSL using Node.js?

I\'ve read the sources of a few drivers (mongojs, mongodb-native) and I\'ve been googling a while now, b

3条回答
  •  情书的邮戳
    2020-12-14 23:26

    If you want to authenticate using a certificate, using node-mongodb-native:

    var buffer = require('fs').readFileSync("mongodb.pem");
    var MongoClient = require('mongodb').MongoClient;
    MongoClient.connect("mongodb://hostname:27017/test?ssl=true", {
        sslKey: buffer,
        sslCert: buffer,
        sslValidate: false //in case of self-generated certificate
    }, function(err, db) {
        console.log(err);
        console.log(db);
        db.close();
    });
    

提交回复
热议问题