title: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017
body: <p>MongoNetworkError: failed to connect to server [localhost:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017</p>
  <p>if i reinstall mongodb,the code works fine. i wanted an permenant solution</p>
  <p>c[error:MongoNetworkError: failed to connect to server [localhost:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017 ]</p>
  <pre><code>const mongoClient=require(&quot;mongodb&quot;).MongoClient
  const state={
      db:null
  }
  
  module.exports.connect=function(done){
      const url=&quot;mongodb://localhost:27017&quot;
      const dbname=&quot;r-kart&quot;
      mongoClient.connect(url,(err,data)=&gt;{
          if(err) return done(err)
          state.db=data.db(dbname)
          done()
      })
  }
  module.exports.get=function(){
      return state.db
  }
  </code></pre>