title: Heroku Node.js (express.js) App Works Locally but failed on heroku when using MongoDB
body: <p>I have an node.js app based on express.js, it works fine on localhost, and it also can connect the Mlab database from localhost:</p>
  
  <pre><code>sheng@Takeshi:~/Work/smr_pjt_loct_dr$ NODE_ENV=production MONGODB_URI=mongodb://heroku_60bjp5jd:PASSWORD@ds115712.mlab.com:15712/heroku_60bjp5jd nodemon start
  [nodemon] 1.11.0
  [nodemon] to restart at any time, enter `rs`
  [nodemon] watching: *.*
  [nodemon] starting `node ./bin/www start`
  test:mongodb://heroku_60bjp5jd:PASSWORD@ds115712.mlab.com:15712/heroku_60bjp5jd
  test the dbURI:mongodb://heroku_60bjp5jd:PASSWORD@ds115712.mlab.com:15712/heroku_60bjp5jd
  Mongoose connected to mongodb://localhost/smr_pjt_loctog
  Mongoose log disconnected
  Db.prototype.authenticate method will no longer be available in the next major release 3.x as MongoDB 3.6 will only allow auth against users in the admin db and will no longer allow multiple credentials on a socket. Please authenticate using MongoClient.connect with auth credentials.
  Mongoose connected to mongodb://heroku_60bjp5jd:PASSWORD@ds115712.mlab.com:15712/heroku_60bjp5jd
  Succeeded connected to: mongodb://heroku_60bjp5jd:PASSWORD@ds115712.mlab.com:15712/heroku_60bjp5jd
  </code></pre>
  
  <p>But when I pushed to heroku, it failed, the error message is shown below:</p>
  
  <pre><code>2017-06-14T11:19:43.117450+00:00 app[web.1]: MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
  </code></pre>
  
  <p>It says that I have failed when connecting to the local server. However, I have set the heroku config var correctly, and as can be seen form below:</p>
  
  <pre><code>sheng@Takeshi:~/Work/smr_pjt_loct_dr$ heroku config
  === smr-pjt-loct-dr Config Vars
  MONGODB_URI: mongodb://heroku_60bjp5jd:PASSWORD@ds115712.mlab.com:15712/heroku_60bjp5jd
  NODE_ENV:    production
  </code></pre>
  
  <p>And as for MLAB database URI, I can get it using code below in the db.js file:</p>
  
  <pre><code>if (process.env.NODE_ENV === 'production') {
  console.log("test:" + process.env.MONGODB_URI);
  dbURI = process.env.MONGODB_URI;
  console.log('test the dbURI:' + dbURI);}
  </code></pre>
  
  <p>I guess the problem is about mongoose, but I have tried many ways, 
  such as, upgrading and specifying the version of Node and NPM in package.json file, or installing the MongoDB driver.</p>
  
  <p>But it cannot work, I have deleted the mongoose connection code below:</p>
  
  <pre><code>mongoose.connect(dbURI, function (err, res) {
    if (err) {
    console.log ('ERROR connecting to: ' + dbURI + '. ' + err);
    } else {
    console.log ('Succeeded connected to: ' + dbURI);
    }
  });
  </code></pre>
  
  <p>And the error message is still the same:</p>
  
  <pre><code>2017-06-14T11:19:43.117450+00:00 app[web.1]: MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
  </code></pre>
  
  <p>And I actually set the Mlab database by using heroku addons correctly as can be seen from <a href="https://i.stack.imgur.com/dyrTb.png" rel="nofollow noreferrer">image description here</a>:</p>
  
  <p>Is there someone can help me? Thanks indeed !!!</p>
  
  <h1>Update:</h1>
  
  <p>As for error message that I have found form heroku logs:</p>
  
  <pre><code>2017-06-14T16:21:49.064801+00:00 app[web.1]: MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
  2017-06-14T16:21:49.064802+00:00 app[web.1]:     at Pool.&lt;anonymous&gt; (/app/node_modules/mongodb-core/lib/topologies/server.js:328:35)
  2017-06-14T16:21:49.064803+00:00 app[web.1]:     at emitOne (events.js:96:13)
  2017-06-14T16:21:49.064804+00:00 app[web.1]:     at Pool.emit (events.js:191:7)
  2017-06-14T16:21:49.064805+00:00 app[web.1]:     at Connection.&lt;anonymous&gt; (/app/node_modules/mongodb-core/lib/connection/pool.js:280:12)
  2017-06-14T16:21:49.064805+00:00 app[web.1]:     at Object.onceWrapper (events.js:293:19)
  2017-06-14T16:21:49.064806+00:00 app[web.1]:     at emitTwo (events.js:106:13)
  2017-06-14T16:21:49.064807+00:00 app[web.1]:     at Connection.emit (events.js:194:7)
  2017-06-14T16:21:49.064808+00:00 app[web.1]:     at Socket.&lt;anonymous&gt; (/app/node_modules/mongodb-core/lib/connection/connection.js:177:49)
  2017-06-14T16:21:49.064808+00:00 app[web.1]:     at Object.onceWrapper (events.js:293:19)
  2017-06-14T16:21:49.064809+00:00 app[web.1]:     at emitOne (events.js:96:13)
  2017-06-14T16:21:49.064809+00:00 app[web.1]:     at Socket.emit (events.js:191:7)
  2017-06-14T16:21:49.064810+00:00 app[web.1]:     at emitErrorNT (net.js:1279:8)
  2017-06-14T16:21:49.064811+00:00 app[web.1]:     at _combinedTickCallback (internal/process/next_tick.js:80:11)
  2017-06-14T16:21:49.064812+00:00 app[web.1]:     at process._tickCallback (internal/process/next_tick.js:104:9)
  </code></pre>
  
  <p>I haven't found anything which is related to the mongose, it seems that the mongodb-core should be responsible for the connection of database, however, I didn't use this npm_module at all. </p>
  
  <p>I have tried to delete the whole node_module folder and use npm install to install them again, but still cannot work.</p>
  
  <p>In addition, I have tried to get the netstat of my localhost:</p>
  
  <pre><code>sheng@Takeshi:~/Work/smr_pjt_loct_dr$ sudo netstat -atunlp | grep "27017"
  tcp        0      0 127.0.0.1:27017         0.0.0.0:*               LISTEN      1034/mongod
  </code></pre>
  
  <h1>Solved:</h1>
  
  <p>Problem resolved, thanks for the help of @vsenko, hahaha!!!
  I have another connection of local database, and I haven't noticed it.</p>