MongoError: socket hang up

2019-04-14 16:36发布

问题:

I am trying to connect to the mongodb database on mongolabs(mlabs). I connect successfully when I run the code on my local computer and server.But When I run on my aws server I get this error database error { [MongoError: socket hang up] name: 'MongoError', message: 'socket hang up' }

Code trial.js:

var express = require('express');
var app = express();
var mongoose = require('mongoose');


var mongojs = require('mongojs');
var db = mongojs('mongodb://user:pass@ds01312192.mlab.com:133492/database', ['mohd'], { ssl : true });

db.on('error', function (err) {
    console.log('database error', err);
});

db.on('connect', function () {
    console.log('database connected');
});

db.mohd.find({}, function (err, docs) { 

    if(err){
        console.log("error");
    }else{
        console.log(docs+"found");
    }

 });

app.set('view engine','ejs');



app.get('/',function(req,res){
  console.log("hi");
});



app.listen(9000,function(){
  console.log("server strated");
});

// catch 404 and forward to error handler

module.exports = app;

Got connection error on Amazon Web Service server but successful on local computer.

回答1:

Ok so I solved the issue it was due to ssl connect method just removed it and was solved. Use Instead:

var db = mongojs('mongodb://user:pass@ds01312192.mlab.com:133492/database', ['mohd']);