#How to make MongoDB work in replit? (Discord Bot)

5 messages · Page 1 of 1 (latest)

fickle cairn

I don't want to keep using the IP 0.0.0.0/0. Basic connection script for pinging:

function connectToServer(callback) {
    try {
        const express = require('express');
        const app = express();

        const startTime = Date.now();

        const server = app.listen(() => {
            const port = server.address().port;
            callback(['SUCCESS', port]);

            app.get('/', async (req, res) => {
                const uptime = Math.floor((Date.now() - startTime) / 1000);
                res.send(`[NEBULA]: Nebula is online with port number ${port}! Uptime: ${uptime} seconds.`);
            });
        });
    }
    catch (error) {
        callback(['GATEWAY_ERROR', error]);
    }
}

module.exports = connectToServer;```
Also, my replit url is `https://nebulajs.cosmocreates.repl.co`. It's hosted on different port numbers, for example:
`[NEBULA]: Nebula is online with port number 61293! Uptime: 0 seconds.`
However, I'm not sure how to implement MongoDB for this, since I keep getting this error:
```MongoServerSelectionError: connection <monitor> to <ip> closed```
Here's the client ready script if you need it in file.

Client Ready Script:

Also I'm a beginner to MongoDB, I just started a week ago and still learning.

NO, this is not Mongoose, this is MongoDB.

const { MongoClient, ServerApiVersion } = require('mongodb');
require('dotenv').config();

let Classified;

if (process.env.REPLIT_ENVIRONMENT !== 'REPLIT') {
    Classified = require('./classified.json');
}
else {
    Classified = require('../../classified.json');
}

const conformat = require('../components/tools/console_formatting.js');

const clients = {};

async function CreateMongoClient(name) {
    try {
        const connectionString = Classified.MongoConnectionToken.replace('[DATABASENAME]', name);
        const mongoClient = new MongoClient(connectionString, {
            serverApi: {
                version: ServerApiVersion.v1,
                strict: true,
                deprecationErrors: true,
            },
        });
        await mongoClient.connect();
        console.log(`${conformat.text.green}[MONGODB]:${conformat.formatting.reset} Connected to the MongoDB database|${name}`);
        clients[name] = mongoClient;
        return mongoClient;
    }
    catch (error) {
        console.error(`${conformat.text.green}[MONGODB]:${conformat.formatting.reset} Couldn't connect to the MongoDB cluster|${name}: ${error}`);
        throw error;
    }
}

function GetMongoClient(name) {
    try {
        const client = clients[name];
        if (client) {
            return client;
        }
        else {
            console.error(`${conformat.text.green}[MONGODB]:${conformat.formatting.reset} Client "${name}" does not exist.`);
            return null;
        }
    }
    catch (error) {
        console.error(`${conformat.text.green}[MONGODB]:${conformat.formatting.reset} Error getting the MongoDB client|${name}: ${error}`);
        throw error;
    }
}

module.exports = {
    CreateMongoClient,
    GetMongoClient,
};