title: Node.js connect to MongoDB throw an error :MongoError: failed to connect to server [127.0.0.1:27017] on first connect
body: <p>I am trying to use node.js to connect with mongodb on my windows system. However, it throw me an error: </p>
  
  <pre><code>C:\Users\mazic\Dropbox\Information System\CS602 Server Site Web Development\MongoDB_Practice\node_modules\mongodb\lib\mongo_client.js:225
            throw err
            ^
  MongoError: failed to connect to server [127.0.0.1:27017] on first connect
      at null.&lt;anonymous&gt; (C:\Users\mazic\Dropbox\Information System\CS602 Server Site Web Development\MongoDB_Practice\node_modules\mongodb\node_modules\mongodb-core\lib\topologies\server.js:313:35)
      at emitOne (events.js:77:13)
      at emit (events.js:169:7)
      at null.&lt;anonymous&gt; (C:\Users\mazic\Dropbox\Information System\CS602 Server Site Web Development\MongoDB_Practice\node_modules\mongodb\node_modules\mongodb-core\lib\connection\pool.js:260:12)
      at g (events.js:260:16)
      at emitTwo (events.js:87:13)
      at emit (events.js:172:7)
      at Socket.&lt;anonymous&gt; (C:\Users\mazic\Dropbox\Information System\CS602 Server Site Web Development\MongoDB_Practice\node_modules\mongodb\node_modules\mongodb-core\lib\connection\connection.js:162:49)
      at Socket.g (events.js:260:16)
      at emitOne (events.js:77:13)
  </code></pre>
  
  <p>and Here is my code: </p>
  
  <pre><code>"use strict";
  const MongoClient = require('mongodb').MongoClient;
  
  const dbUrl = 'mongodb://127.0.0.1:27017/cs602';
  
  MongoClient.connect(dbUrl,  (err, db) =&gt; {
      if (err)
      {throw err}else {
          console.log('Successfully connected to',
              db.s.databaseName);
      }
      // Do database operations
      db.close();
  });
  </code></pre>
  
  <p>Here is my file structure:
  <a href="https://i.stack.imgur.com/dw37d.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dw37d.png" alt="enter image description here"></a></p>
  
  <p>I am new to node.js as well as mongodb, does anyone can help me with this? Thank you so much in advance!</p>