我已经创建了一个启用了SSL的DocumentDB,并且我使用mongodb
包使用NodeJS
来使用Bastion
主机连接这个DB。问题是,如果我在MongoClient.connect
函数中放置一个硬编码字符串,我就能够成功地连接DB。硬编码的代码如下所示。
let MongoClient = require('mongodb').MongoClient;
let client = MongoClient.connect(
'mongodb://User:PWD@DBURL:27017/DBNAME?tls=true&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false',
{
tlsCAFile: __dirname + `rds-combined-ca-bundle.pem` //Specify the DocDB; cert
},
function(err, client) {
if(err)
throw err;
console.log("1111111 2222222!!");
//Specify the database to be used
db = client.db('DBNAME');
//Specify the collection to be used
col = db.collection('COLNAME');
console.log("1111111 connected to db!!");
client.close();
});
现在,因为这并不是将硬编码的值放入代码中的理想情况。我试图从环境变量中读取值,并尝试将整个URL放入一个字符串变量中,并将该变量传递到该函数中,如下所示。
const DBURL = "mongodb://"+user+":"+pwd+"@"+dbURL+":"+port+"/"+dbName+"?tls=true&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false";
let client = MongoClient.connect(DBURL,
{
tlsCAFile: __dirname + `rds-combined-ca-bundle.pem` //Specify the DocDB; cert
},
function(err, client) {
现在,这一次连接DB。
关于它的任何建议,或者我是否应该使用任何其他包通过NodeJS连接NodeJS,请告诉我。
发布于 2022-03-01 13:45:50
像这样的东西对我有用:
const { MongoClient } = require('mongodb')
const DOCUMENTDB_CONNECTION_STRING = `mongodb://${process.env.DOCDB_USER}:${process.env.DOCDB_PASS}@${process.env.DOCDB_ENDPOINT}/${process.env.DOCDB_DBNAME}?tls=true&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false`
const client = MongoClient.connect(
DOCUMENTDB_CONNECTION_STRING, {
tlsCAFile: `rds-combined-ca-bundle.pem` //Specify the DocDB cert
},
function(err, client) {
这就是你要找的吗?
https://stackoverflow.com/questions/70313758
复制相似问题