Hi everyone.
I think I have a very specific issue.
I created my first ever website with Node.js in the back-end and a MongoDB Atlas cluster as a database.
I hosted my web app on Heroku where everything works fine. A few days ago when I got everything launch-ready I decided to pay for proper hosting and buy the domain I wanted.
My host server uses cPanel where it is possible to setup a Node.js app, which is what I did. However my website is not loading because I cannot establish a connection to my Atlas cluster.
It’s the exact same code that works fine on Heroku, so I’m not sure it has to do with MongoDB itself. Perhaps it’s some security setting inside cPanel that prevents this connection but I was not able to find any.
With the ‘useUnifiedTopology’ flag set to true I get a MongooseServerSelectionError. I saw some people having issues establishing a connection with this flag, so I tried commenting it out and now get a MongoNetworkError instead.
Here’s my code:
const connectionString = 'mongodb+srv://' + process.env.MONGODB_USER + ':' + process.env.MONGODB_PW + '@<REDACTED_APP>-y1llv.mongodb.net/<REDACTED_APP>';
mongoose
.connect(connectionString, {
useUnifiedTopology: true,
useNewUrlParser: true,
useFindAndModify: false
})
.then(() => console.log('Database connected.'))
.catch(err => console.log(err));
You can see a picture of my cPanel Node.js app setup and the complete callstack for these errors on my StackOverflow post here.
I really hope someone can help me out. It’s my first website and the first time I host anything, so I might very well miss something but I don’t know where or what to look for anymore. I’m pretty much stuck.