title: MongoError: failed to connect to server [localhost:27017] on first connect (ECONNREFUSED)
body: <p>I just added MongoDB to the dependencies of my Node.js project created with the <code>npm init</code> command. My index.js code is:</p>
  
  <pre><code>var MongoClient = require('mongodb').MongoClient
  , assert = require('assert');
  
  // Connection URL
  var url = 'mongodb://localhost:27017/mydatabase';
  
  // Use connect method to connect to the server
  MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);
  console.log("Connected successfully to server");
  
  db.close();
  });
  </code></pre>
  
  <p>But when I execute the code it throws the following error:</p>
  
  <pre><code>AssertionError: null == { MongoError: failed to connect to server [localhost:27017] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27017]
  </code></pre>
  
  <p>I really don't know how to fix it. I followed some guides that reported the same error but I couldn't fix it.</p>
  
  <p>Should I poen a port on my modem? Should I replace "localhost" with my IP? Should I do anything else?</p>
  
  <p>Please help me!</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I installed a MongoDB server on my Android device and replacing the <code>url</code> variable with <code>mongodb://ANDROID-DEVICE-LOCAL-IP:27017/mydatabase</code> it now works.</p>
  
  <p>How can I accomplish it placing the database on my computer? Is the firewall blocking incoming connections? Is this why it works on Android but not on Windows?</p>