Recommand · June 6, 2021 0

Local mongodb Node.js server succesfully authenticates then ends connections after a few seconds

I am trying to run an Express server that would communicate with a local MongoDB instance. Everything runs on Ubuntu 18.04.5 on DigitalOcean.

I managed to set up the database, the authentication is made, but then after a few seconds the connection closes due to some mongodb dependencies (I think).

This is how I make the connection:

const httpsServer = https.createServer(credentials, app);

const mongooseConnection = mongoose
                          .createConnection(
                            `mongodb://user:pass@127.0.0.1:27017/Database?retryWrites=true&w=majority`,
                            { useNewUrlParser: true, useUnifiedTopology: true }
                          );

mongooseConnection.on('error', console.error.bind(console, 'Connection error: '));

mongooseConnection.once('open', () => {
  console.log("Connected to the DB");
  httpsServer.listen(securePort, () => {
    console.log(`Secure Express server listening on port ${securePort}`);
  });
});

Node console:

This is the result:

Mongod:

enter image description here

Couldn’t find much about this error. Most of what I’ve found is related to the MongoDB Atlas cloud service, but this is a local MongoDB instance. The common solution for when the connection closes like this seems to be whitelisting the IP. I think this is not the case as everything is local and I have nothing to do with the Atlas/Cluster.

I tried removing the node modules and updating them. I tried disabling cors and helmet. I’m stuck