使用Node.js通过SSL连接到MongoDB [英] Connecting to MongoDB over SSL with Node.js

查看:126
本文介绍了使用Node.js通过SSL连接到MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用Node.js通过SSL连接到MongoDB服务器?

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

我已经阅读了几个驱动程序的来源( mongojs mongodb-native )我现在一直在谷歌搜索,但似乎找不到任何适当的教程,指南或文档。

I've read the sources of a few drivers (mongojs, mongodb-native) and I've been googling a while now, but can't seem to find any proper tutorials, guides or docs.

推荐答案

根据评论中的建议, node-mongodb-native 包含所需的一切。

As suggested in the comments, the node-mongodb-native has everything needed.

我使用以下内容运行并运行:

I got it up and running using the following:

var mongo = require('mongodb');

var server = new mongo.Server('HOSTNAME', 27017, { ssl: true });
var db = new mongo.Db('NAME_OF_MY_DB', server, { w: 1 });
var auth = { user: 'USERNAME', pass: 'PASSWORD' };

db.open(function(err, db) {
  if (err) return console.log("error opening", err);

  db.authenticate(auth.user, auth.pass, function(err, result) {
    if (err) return console.log("error authenticating", err);

    console.log("authed?", result);

    db.collection('whatever').count(function(err, count) {
      if (err) return console.log("error counting", err);

      console.log("count", count);
      db.close()
    });
  });
});

修改

你也可以从 mongoose 做ssl:

mongoose.createConnection(connString, { server: { ssl: true }})

这篇关于使用Node.js通过SSL连接到MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆