ssh tunnel connect to mongodb in node js

Solutions on MaxInterview for ssh tunnel connect to mongodb in node js by the best coders in the world

showing results for - "ssh tunnel connect to mongodb in node js"
David
18 Apr 2017
1var config = {
2    username:'myusername',
3    host:'my.ip.address',
4    agent : process.env.SSH_AUTH_SOCK,
5    privateKey:require('fs').readFileSync('/Users/myusername/.ssh/id_rsa'),
6    port:22,
7    dstPort:27017,
8    password:'mypassword'
9};
10
11var server = tunnel(config, function (error, server) {
12    if(error){
13        console.log("SSH connection error: " + error);
14    }
15    mongoose.connect('mongodb://localhost:27017/mydbname');
16
17    var db = mongoose.connection;
18    db.on('error', console.error.bind(console, 'DB connection error:'));
19    db.once('open', function() {
20        // we're connected!
21        console.log("DB connection successful");
22    });
23});
24