When trying to connect my Node app to MongoDB, no errors appear in the terminal. However, localhost:3000 refuses to connect. I’ve verified the server is running and listening on the correct port. The IP whitelisting in MongoDB Atlas is set to 0.0.0.0/0. Despite these checks, the connection issue persists. I used netstat -ano | findstr :3000 to check if the port is listening, and it showed the port is not in use. Additionally, the terminal does not display “Server is running on http://localhost:3000”. When I use the command curl http://localhost:3000, it shows curl: (7) Failed to connect to localhost port 3000 after 2251 ms: Couldn’t connect to server.
let express = require("express");
let mongodb = require("mongodb");
let app = express();
let db;
let MongoClient = mongodb.MongoClient;
let connectionString =
"mongodb+srv://nithyananthankrishnan:<password>@cluster0.ooz4hi1.mongodb.net/ToDoist?retryWrites=true&w=majority&appName=Cluster0";
MongoClient.connect(
connectionString,
function (err, client) {
if (err) {
console.error("Failed to connect to the database:", err.message);
return;
}
db = client.db();
console.log("Database connected successfully.");
app.listen(3000, () => {
console.log("Server is running on http://localhost:3000");
});
}
);
app.use(express.urlencoded({ extended: false }));
app.get("/", function (req, res) {
res.send('hello world');
});
app.post("/create-item", function (req, res) {
db.collection("items").insertOne({ text: req.body.item }, function () {
res.send("thanks for submitting the form");
});
});
- i tried different port number like 3001
- tried to trun off firewall
but nothing is working for me