Error: Timed out while waiting for handshake - Facing this issue while accessing mongodb from nodejs app deployed on Digitlal Ocean

Viewed 1200

I have a droplet of mongodb which I have created using SSH key. Then, I have created an app of NodeJs on digital ocean. (Configured it using github repo). In that app, I am using that mongodb droplet database. I am facing this handshake issue while accessing the database in my nodeJs App. I have tried both the ways:

a) To place the sshkey in the github repo

let mongoConfigArray = {
    username: 'root',
    connectTimeout: 99999,
    host: dropletIpAddress,
    privateKey: require('fs').readFileSync('./config/mysshkey'),
    secure : true,
    port: 22,
    dstPort: 27017,
};

let connectionString = 'mongodb://user:pwd@localhost:27017/dbname';

var server = tunnel(mongoConfigArray, function (error, server) {
    if(error){
        console.log("SSH connection error: " + error);
    }
    mongoose.connect(connectionString, { useCreateIndex: true, useNewUrlParser: true, useUnifiedTopology: true });

    var db = mongoose.connection;
    db.on('error', console.error.bind(console, 'DB connection error:'));
    db.once('open', function() {
        // we're connected!
        console.log("DB connection successful");
    });
});

b) Or to place it somewhere online and accessing it when app runs.

request.get(fileUrl, function (error, response, body) {
    if (!error && response.statusCode == 200) {
        // console.log(body);
        mongoConfigArray.privateKey = body;
        tunnel(mongoConfigArray, function (error, server) {
            if(error){
                console.log("SSH connection error: " + error);
            }
            mongoose.connect(connectionString, { useCreateIndex: true, useNewUrlParser: true, useUnifiedTopology: true });
        
            var db = mongoose.connection;
            db.on('error', console.error.bind(console, 'DB connection error:'));
            db.once('open', function() {
                // we're connected!
                console.log("DB connection successful");
            });
        });
    }
});

But I coudn't able to succeed it on the digital ocean app, while both of the ways working perfectly fine on local. Will be very thankfull if anyone knew how to resolve this issue. Thanks

0 Answers
Related