0

I am trying to connect my mongodb server with express. But the server is not listening when i am giving the listen function inside connectToServer(). The following snippet is the index.js file.

const express = require("express");
const { connectToServer } = require("./utils/dbConnect");
const usersRoute=require('./routes/users.route.js');
const app = express();
const port = 5000;
connectToServer((err) => {
 
    app.listen(port, () => {
      console.log({ port });
    }
    
)});
app.use('/users',usersRoute)
app.get("/", (req, res) => {
  res.send("Hello World");
});

Here is the dbConnect.js snippet:

const { MongoClient } = require("mongodb");
const connectionString = "mongodb://localhost:27017";
const client = new MongoClient(connectionString, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

let dbConnection;

module.exports = {
  connectToServer: function (callback) {
    client.connect(function (err, db) {
      if (err || !db) {
        return callback(err);
      }

      dbConnection = db.db("users");
      console.log("Successfully connected to MongoDB.");

      return callback();
    });
  },

  getDb: function () {
    return dbConnection;
  },
};


The server stucks at [nodemon] starting node index.js

I was expecting to get the server running and listening. But it doesn't.

  • 2
    Can you connect to the local mongod using mongosh or compass? – Joe Feb 15 '23 at 21:26
  • This would appear to be more of a "mongo is not listening where I expect" issue than a "my code cannot connect to mongo" issue. Perhaps we should inspect the mongo config? – PaulProgrammer Feb 15 '23 at 21:26
  • @Joe yes. compass connection is working seamlessly and i can do commands on mongosh shell. it just not working with node.js – Tanim Istiak Feb 15 '23 at 21:27
  • ECONNREFUSED means the operating system actively refused the connection because nothing is listening on that port. In the sample error message, it looks like it is trying with the IPv6 localhost. Is the error message different when using 127.0.0.1 in the connection string? – Joe Feb 15 '23 at 21:29
  • If i set the connectionString to mongodb://127.0.0.1:27017, the console window stucks at [nodemon] starting `node index.js` forever. But no error message – Tanim Istiak Feb 15 '23 at 21:30
  • Isn't that what it should do? Is it listening on port 5000? – Joe Feb 15 '23 at 21:40
  • Yes. The server port is 5000. But it is not listening. i can't see this line output. console.log({ port }); – Tanim Istiak Feb 15 '23 at 21:44
  • Does this answer your question? [Can't connect to MongoDB 6.0 Server locally using Nodejs driver](https://stackoverflow.com/questions/74609210/cant-connect-to-mongodb-6-0-server-locally-using-nodejs-driver) – Wernfried Domscheit Feb 15 '23 at 21:57
  • @WernfriedDomscheit now the server is not crushing. But it’s still not listening. – Tanim Istiak Feb 15 '23 at 22:14
  • Do you still get the same error? I don't think so, in this case you may open a new question or at least edit your question and update with latest error message. – Wernfried Domscheit Feb 16 '23 at 07:38
  • @WernfriedDomscheit there is no error now. Just the server is stuck at starting. The console log inside the listen function is not appearing. – Tanim Istiak Feb 16 '23 at 09:07

1 Answers1

1

Here is the dbConnect() snippet.

const { MongoClient } = require("mongodb-legacy");
const connectionString = "mongodb://localhost:27017";
const client = new MongoClient(connectionString, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

let dbConnection;

module.exports = {
  connectToServer: function (callback) {
    client.connect(function (err, db) {
      if (err || !db) {
        return callback(err);
      }

      dbConnection = db.db("users");
      console.log("Successfully connected to MongoDB.");

      return callback();
    });
  },

  getDb: function () {
    return dbConnection;
  },
};

Here the callback function client.connect() is deprecated since mongodb v5. So i used another package to support the legacy mongodb drivers from here: Legacy-Driver

That package on github says:

The next major release of the driver (v5) will drop support for callbacks. This package will also have a major release at that time to update the dependency requirement to ^5.0.0. Users can expect to be able to upgrade to v5 adopting the changes and features shipped in that version while using this module to maintain any callback code they still need to work on migrating.

Lastly i changed the MongoClient location to this:

const { MongoClient } = require("mongodb-legacy");