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

Hi Samuel,

Thank you for the detailed description of the issue!

Are you connecting to a local MongoDB instance? If so, have you considered connecting to a free MongoDB Atlas cluster instead? This could help determine if the problem is with the local MongoDB installation.

Otherwise, your code looks good.

Best,
Mira

Hello,
Thanks for looking into this.
My URI is to a atlas cluster, correct.

Im surprised by your response. -I read the mongoose documentation on keepalive requiring users to exporting schemas and not export models to have > 1 connection.

Once I implemented a global db connect function, required it in my app.js and routers, exported my schemas, and connection.model-ed everything, I got it working.
Based on the documentation I thought somehow the scope of the mongoose.connect was dropping during calls to my router.

I know you’re not mongoose support, but something seemed to change that broke the project exporting models rather than schemas

Does anyone know when and why changes were implemented that would require me to make the changes I described above?

I am 99% certain I populated my entire database with these routers without conflict, it’s been a while since I worked on the app so I could’ve missed out on some mongoose changes.
I also posted this question here