MongoDb和Node.js SSl/安全连接 [英] MongoDb and nodejs SSl/Secure Connection

查看:100
本文介绍了MongoDb和Node.js SSl/安全连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过ssl连接mongoDB和nodejs,
我正在使用此代码创建连接,但无法正常工作

How can i connect mongoDB and nodejs over ssl,
i am using this code for creating connection but it is not working

var Db = require('mongodb').Db;
var Server = require('mongodb').Server;
Db.connect('mongodb://xxx.xxx.xxx.xxx:27017/db-login', { auto_reconnect: true, poolSize:4, ssl:true }, function (err, db) {

我还尝试了另一个代码

 var localIP='xxx.xxx.xxx.xxx:27017', ssl=true;

任何帮助和建议

推荐答案

使用以下链接在mongoDB环境中配置SSL.

Use the links below to configure SSL in your mongoDB environment.

必须在您的MongoDB环境中启用SSL,并且必须在您的应用程序中启用SSL.

SSL has to be enabled in your MongoDB environment, and SSL has to be enabled in your application.

http://docs.mongodb.org/manual/tutorial/configure-ssl / http://docs.mongodb.org/manual/tutorial /configure-ssl-clients/

您可以按照以下步骤在节点中配置SSL连接:

You can configure a SSL connection in node as followed:

var options = {
    key: fs.readFileSync('ssl/your_ssl_key.key'),
    cert: fs.readFileSync('ssl/your_ssl_cert.cert')
};

https.createServer(options, app).listen(443);

如果要对所有用户强制使用https连接,则可以强制重定向:

If you want to force a https connection for all users, you can force a redirect:

    require("http").createServer(function (req, res) {
    res.writeHead(301, {
        'Content-Type': 'text/plain',
        'Location': 'https://' + req.headers.host + req.url
    });
    res.end('Redirecting to SSL\n');
}).listen(80);

https.createServer(options, app).listen(443);

这篇关于MongoDb和Node.js SSl/安全连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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