title: Connect NodeJS to MongoDB Droplet
body: <p>So I've decided to set up my project with two droplets, a MongoDB image droplet and a NodeJS image droplet. I did so to make it easier to scale both droplets in the future, and other applications may be connecting to the DB in the future, both on Ubuntu 18.04.</p>
  
  <p>I'm getting the error of:</p>
  
  <pre><code>Could not connect to the database:  { MongooseServerSelectionError: connection timed out
      at new MongooseServerSelectionError (/root/eternal-peace-code/node_modules/mongoose/lib/error/serverSelection.js:22:11)
      at NativeConnection.Connection.openUri (/root/eternal-peace-code/node_modules/mongoose/lib/connection.js:808:32)
      at Mongoose.connect (/root/eternal-peace-code/node_modules/mongoose/lib/index.js:333:15)
      at Timeout.setTimeout [as _onTimeout] (/root/eternal-peace-code/app.js:60:22)
      at ontimeout (timers.js:482:11)
      at tryOnTimeout (timers.js:317:5)
      at Timer.listOnTimeout (timers.js:277:5)
    message: 'connection timed out',
    name: 'MongooseServerSelectionError',
    reason: 
     TopologyDescription {
       type: 'Single',
       setName: null,
       maxSetVersion: null,
       maxElectionId: null,
       servers: Map { 'mongo_droplet_ip:27017' =&gt; [Object] },
       stale: false,
       compatible: true,
       compatibilityError: null,
       logicalSessionTimeoutMinutes: null,
       heartbeatFrequencyMS: 10000,
       localThresholdMS: 15,
       commonWireVersion: null },
    [Symbol(mongoErrorContextSymbol)]: {} }
  </code></pre>
  
  <p>I have set up both servers correctly (so I believe), my MongoDB has two users (both are me but different permissions, I have followed several walkthroughs and so it just happened). My /etc/mongod.conf file has been edited accordingly:</p>
  
  <pre><code>net:
    port: 27017
    bindIp: 127.0.0.1,mongodb_droplet_ip
  security:
    authorization: "enabled"
  
  </code></pre>
  
  <p>The UFW firewall has HTTPS, SSH and my NodeJS_ip:27017 enabled.</p>
  
  <p>My NodeJS droplet is set up with a domain name pointing to the right IP address, letsencrypt is set up for secure connection at the domain name. The issue I have now is that I can not connect to my Mongo droplet from my NodeJS application, and I would also like to make sure that it is all done securely too.
  My NodeJS connect code is using Mongoose and a variables environment file, I also have the whole connect in a timeout function as per another suggestion I saw elsewhere:</p>
  
  <pre><code>setTimeout(async () =&gt; {
        console.log('Connecting to database...');
        const options = {
          user: process.env.DBUSER,
          pass: process.env.USERPASS,
          keepAlive: true,
          useNewUrlParser: true,
          useFindAndModify: false,
          useCreateIndex: true,
          useUnifiedTopology: true,
          sslCA: cert,
          connectTimeoutMS: 5000,
        }
        await mongoose.connect(process.env.LIVEDB, options)
        .then(() =&gt; {
          console.log('We are connected to the database');
        })
        .catch(err =&gt; {
          console.log('Could not connect to the database: ', err);
          connectWithTimeout();
          });
      }, 3000);
  </code></pre>
  
  <p>I have tried a multitude of things through both droplets but I have wasted around 4 days on getting the project to a staging/production stage so it can launch whenever and just be updated as time goes on.</p>
  
  <p>If you need anything else, please do let me know.</p>
  
  <p>All help would be hugely appreciated,</p>
  
  <p>Thanks</p>