psmod2
By:
psmod2

Unable to Connect to MongoDB in Node App

January 22, 2017 300 views
MongoDB Node.js

Hi,

I'm trying to get my Node app (separate droplet) to connect to my mongoDB droplet.

I've found a npm called tunnel-ssh however am having trouble.

It says "DB connection successful", however data is not coming back. And when I do a console.log(mongoose) it shows the host and host as null.

If I do console.log(mongoose), after the console.log("DB connection successful"); then it shows me the host.

Anyhow, my code looks like:

var tunnel = require('tunnel-ssh');

var config = {
agent : 'myuser',
host: 'xxx:xxx:xxx:xxx'
agent : process.env.SSHAUTHSOCK,
privateKey:require('fs').readFileSync('id_rsa'),
port:22,
dstPort:27010,
keepAlive: true
};

var server = tunnel(config, function (error, server) {

if(error){
    console.log("SSH connection error: " + error);
}

mongoose.connect('mongodb://127.0.0.1:27017/mysuperdb');

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

});

Be the first one to answer this question.