MongoClient.connect not working from nodejs

Hi,
I am a beginner in mongodb, trying to establish a connection to mongodb from nodejs. There is no error and it just hangs in the terminal window in VS code.

I set up the environment variable Path to C:\Program Files\MongoDB\Server\6.0\bin
and ran ‘npm install mongodb’.
“mongodb://localhost:27017/” is running in my local docker desktop.
“mongodb://localhost:27017/” succcessfully connects from Mongodb for VS code tool but not connecting from VS code nodejs. Below is my code and any help would be greatly appreciated!!

const MongoClient = require("mongodb").MongoClient;
const url = "mongodb://localhost:27017/";
//const url = "mongodb://127.0.0.1:27017";

MongoClient.connect(url, { useUnifiedTopology: true }, function (err, client) {
  if (err) {
    console.log("err");
  } else {
    console.log("Database Connected");
  }
});

Thanks
Chitra

Did you try with the line 127.0.0.1 which is commented in your code?

Hi,
Thanks for your response. Yes I did try 127.0.0.1 but not connecting. I am seeing below errors in the mongod log files.

“msg”:“Failed to check socket connectivity”,“attr”:{“error”:{“code”:6,“codeName”:“HostUnreachable”,“errmsg”:“Connection reset by peer”}}}
“msg”:“Interrupted operation as its client disconnected”,

Thanks
Chitra