I apologize for revisiting this topic, but I would appreciate your assistance. I’ve been working on a project intermittently for some time now. Recently, after transitioning to a new computer, none of my endpoints seem to be functioning properly.
I’ve taken several troubleshooting steps including:
- Deleting
node_modules and reinstalling dependencies with npm.
- Adjusting inbound and outbound firewall rules for ports 27016 and 27017.
- Attempting to listen on ports 27016 and 27017 in my application setup.
- Starting a new project from scratch.
- Adding extensive console logging for debugging purposes.
- Successfully connecting to MongoDB using both MongoDB GUI and VS Code extensions; the initial connection is established, and I can list databases and collections without issue.
However, the connection appears to drop unexpectedly when I make requests to my endpoints. Your insights on resolving this issue would be greatly appreciated.
I have no idea what has changed or why this project is failing. Other discussion threads discuss using models before mongodb initialization, but in my current understanding, I am not doing that.
to simplify my app:
My app.js does the following:
//init
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const dotenv = require('dotenv/config');
/requires
const mineralsRouter = require('./backend/routers/minerals');
const usersRouter = require('./backend/routers/users');
const ordersRouter = require('./backend/routers/orders');
const categoriesRouter = require('./backend/routers/categories');
const api = process.env.API_URL;
//routers
app.use(`${api}/users`, usersRouter);
app.use(`${api}/inventory`, mineralsRouter);
app.use(`${api}/orders`, ordersRouter);
app.use(`${api}/categories`, categoriesRouter);
mongoose.set('debug', true); // Enable Mongoose debug mode
mongoose.set('bufferCommands', false); //
mongoose.connect(process.env.MINERALS_CONNECT, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverSelectionTimeoutMS: 30000,
socketTimeoutMS: 45000
}).then( async ()=>{
console.log('connected to: MINERALS')
const connection = mongoose.connection;
// Example: List all database names
const adminDb = connection.db.admin();
const databaseNames = await adminDb.listDatabases();
console.log('Available databases:', databaseNames.databases);
app.listen(27016, () => {
console.log(api);
console.log('Server running at: http://localhost:27016');
})
})
The following works.
but when I GET to {api}/users/all (or any other path)
it times out
user-router:
const mongoose = require('mongoose');
const { User } = require('../models/user');
const express = require('express');
const router = express.Router();
//get all users
router.get('/all', async (req, res) => {
try {
// Fetch all users
const users = await User.find({});
console.log(users);
// Respond with the list of users
res.send(users);
} catch (error) {
console.error('Error fetching users:', error);
res.status(500).json({ error: error.message });
}
});
I can see that my connection is dropped by calling:
router.get(‘/check-connection’, async (req, res) => {
try {
// Ensure the connection is established
if (mongoose.connection.readyState !== 1) {
throw new Error(‘MongoDB connection not established’);
}
// Perform a basic query to the MongoDB server
const result = await mongoose.connection.db.admin().ping();
console.log('MongoDB Ping Result:', result);
res.status(200).send('Connection to MongoDB is successful');
} catch (error) {
console.error(‘Error pinging MongoDB:’, error);
res.status(500).json({ error: 'Error connecting to MongoDB: ’ + error.message });
}
});
any help is appreciated. Thank you sm