我正在尝试使用函数中的tunnel-ssh
访问服务器上外部托管的MongoDB数据库。运行隧道功能时没有出现任何错误,运行功能const client = await connectToMongodb(client2);
时到达getDataFromMongoDB
时似乎停滞了。当我在没有SSH的情况下在本地运行此程序时,一切正常,因此我认为这与SSH隧道组件有关。有人可以告诉我我在做什么错。
var config = {
username: "myusername",
privateKey: require("fs").readFileSync("../id_rsa"),
host: "207.138.88.161",
port: 22,
dstHost: "11.12.97.2",
dstPort: 27017,
localHost: "127.0.0.1",
localPort: 27000
};
tunnel(config, function(error, server) {
//....
if (error) {
console.log(error);
}
console.log(server);
getDataFromMongoDB();
});
async function getDataFromMongoDB(page) {
const MongoClient = require("mongodb").MongoClient;
const uri = "mongodb://username:password@11.12.97.2:27017/mydatabase";
const client2 = new MongoClient(uri, { useNewUrlParser: true });
const client = await connectToMongodb(client2);
const collection = client.db("mydatabase").collection("jobs");
const jobs = await collection.find().toArray();
client.close();
client2.close();
console.log(jobs);
}
function connectToMongodb(client) {
return new Promise((resolve, reject) => {
client.connect(function(err) {
return resolve(client);
});
});
}