Skip to content

Instantly share code, notes, and snippets.

@sharif2008
Created April 6, 2018 13:38
Show Gist options
  • Save sharif2008/48c4289198d5f46d6d45c8bef5887ca7 to your computer and use it in GitHub Desktop.
Save sharif2008/48c4289198d5f46d6d45c8bef5887ca7 to your computer and use it in GitHub Desktop.
Connection error on starting heroku (Node.js/Express + MongoLab)

I created question on StackOverflow

When trying to start Node.js app hosted on Heroku with MongoLab addons, I got following error on heroku logs when connecting to MongoDB.

My entire vode is here: https://github.com/yhagio/meetup_planner

Heroku logs

2015-11-28T02:03:08.527236+00:00 heroku[web.1]: State changed from starting to crashed
2015-11-28T02:03:08.528487+00:00 heroku[web.1]: State changed from crashed to starting
2015-11-28T02:03:10.052621+00:00 heroku[web.1]: Starting process with command `node server.js`
2015-11-28T02:03:10.073414+00:00 heroku[web.1]: Starting process with command `node server.js`
2015-11-28T02:03:13.445146+00:00 app[web.1]: Server started at port number:  57859
2015-11-28T02:03:13.393279+00:00 app[web.1]: Connecting to DB :  mongodb://USERNAME:[email protected]:59804/heroku_bt6frdh6
2015-11-28T02:03:13.467692+00:00 app[web.1]:
2015-11-28T02:03:13.467698+00:00 app[web.1]: /app/node_modules/mongodb/lib/server.js:235
2015-11-28T02:03:13.467699+00:00 app[web.1]:         process.nextTick(function() { throw err; })
2015-11-28T02:03:13.467700+00:00 app[web.1]:                                       ^
2015-11-28T02:03:13.467705+00:00 app[web.1]:     at Object.exports._errnoException (util.js:860:11)
2015-11-28T02:03:13.467704+00:00 app[web.1]: Error: connect ECONNREFUSED 127.0.0.1:27017
2015-11-28T02:03:13.467705+00:00 app[web.1]:     at exports._exceptionWithHostPort (util.js:883:20)
2015-11-28T02:03:13.467706+00:00 app[web.1]:     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
2015-11-28T02:03:13.496341+00:00 app[web.1]: Server started at port number:  19421
2015-11-28T02:03:13.447621+00:00 app[web.1]: Connecting to DB :  mongodb://USERNAME:[email protected]:59804/heroku_bt6frdh6
2015-11-28T02:03:13.532176+00:00 app[web.1]:
2015-11-28T02:03:13.532180+00:00 app[web.1]: /app/node_modules/mongodb/lib/server.js:235
2015-11-28T02:03:13.532181+00:00 app[web.1]:         process.nextTick(function() { throw err; })
2015-11-28T02:03:13.532182+00:00 app[web.1]:                                       ^
2015-11-28T02:03:13.532186+00:00 app[web.1]: Error: connect ECONNREFUSED 127.0.0.1:27017
2015-11-28T02:03:13.532187+00:00 app[web.1]:     at Object.exports._errnoException (util.js:860:11)
2015-11-28T02:03:13.532187+00:00 app[web.1]:     at exports._exceptionWithHostPort (util.js:883:20)
2015-11-28T02:03:13.532188+00:00 app[web.1]:     at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1063:14)
2015-11-28T02:03:14.171410+00:00 heroku[web.1]: Process exited with status 1
2015-11-28T02:03:14.452421+00:00 heroku[web.1]: Process exited with status 1
2015-11-28T02:03:14.466128+00:00 heroku[web.1]: State changed from starting to crashed
2015-11-28T02:03:16.108198+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=warm-retreat-5113.herokuapp.com request_id=514e9630-6c5f-4b61-8a46-e9fb282a0704 fwd="166.62.227.154" dyno= connect= service= status=503 bytes=
2015-11-28T02:03:16.394773+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=warm-retreat-5113.herokuapp.com request_id=7138fc98-0c63-4a52-a4ba-063ef03d5e95 fwd="166.62.227.154" dyno= connect= service= status=503 bytes=

Here is the code where making a cnnection to MongoDB (MongoLab addons from Heroku) server/db_connection.js

var mongoose  = require('mongoose');
var config    = require('../express-config');
var userModel = require('./models/user');
var eventModel = require('./models/event');

function makeDefaultConnection() {
  var uri = process.env.MONGOLAB_URI || '127.0.0.1/meetupplanner';
  console.log('Connecting to DB : ', uri);
  var conn = mongoose.createConnection(uri, function(err){
    if(err){
      console.log('CONNECTION ERROR::::', err);
    }
    console.log('CONNECTED to DB!');
  });
  conn.model('User', userModel.userSchema);
  conn.model('Event', eventModel.eventSchema);
  return conn;
}

module.exports.defaultConnection = makeDefaultConnection();

Procfile

web: node server.js

in Server.js

...
var port = process.env.PORT || 3000;
app.listen(port, function() {
  console.log('Server started at port number: ', port);
});

After I added MongoLab Addons on Heroku, I verified that my MONGOLAB_URI is set on Heroku (Settings > Config Variables)

Reference: https://devcenter.heroku.com/articles/mongolab

Similar issues asked on StackOverflow

Note that I could connect to mongo shell via

$ mongo ds059804.mongolab.com:59804/heroku_tb6frdh6 -u USERNAME -p PASSWORD

So, I think credentials are fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment