title: How to fix 'Error: querySrv EREFUSED' when connecting to MongoDB Atlas?
body: <p>I am new to MongoDB 4.0.6 and tried to implement it into my website using Node/Express.js, but when I try to connect to <code>mongodb+srv://${process.env.MONGOUSER}:${process.env.MONGOPASS}@main-03xkr.mongodb.net/main</code> I'm getting this error:</p>
  
  <blockquote>
    <p>{ Error: querySrv EREFUSED _mongodb._tcp.main-03xkr.mongodb.net
        at QueryReqWrap.onresolve [as oncomplete] (dns.js:199:19)
      errno: 'EREFUSED',
      code: 'EREFUSED',
      syscall: 'querySrv',
      hostname: '_mongodb._tcp.main-03xkr.mongodb.net' }</p>
  </blockquote>
  
  <p>I've tried connecting to <code>mongodb://localhost:27017/main</code>, but this <strong>does</strong> seem work.</p>
  
  <p>Here is the relevant code:</p>
  
  <pre><code>require('dotenv').config();
  const mongoose = require('mongoose');
  
  // Database
  const uri = `mongodb+srv://${process.env.MONGOUSER}:${process.env.MONGOPASS}@main-03xkr.mongodb.net/main`;
  const localURI = 'mongodb://localhost:27017/main';
  
  var Project = require('./models/project');
  
  mongoose.connect(uri, { useNewUrlParser: true });
  const db = mongoose.connection;
  
  db.once('open', () =&gt; console.log('Successfully connected to MongoDB'));
  db.on('error', (e) =&gt; console.log(e));
  
  // Routes
  app.get('/', (req, res) =&gt; {
    Project.find({}, (e, projects) =&gt; {
      if (e) console.log(e);
  
      res.render('home.ejs', {
        projects: projects
      });
    });
  });
  
  </code></pre>
  
  <p>So does anyone know how to fix this error and maybe explain what is happening here?</p>