0

I've created a simple Node.js project that uses MongoDB for storing data. Locally, everything works perfectly fine, but when I try to host my app on platforms like Replit or Heroku, I encounter MongoDB connection issues. The project is part of my attempt to pass the FreeCodeCamp tests for the "URL Shortener Microservice" project.

Here's a snippet of my code:

// MongoDB connection setup
require('dotenv').config();
const { MongoClient } = require('mongodb');
const client = new MongoClient(process.env.DB_URI);
const db = client.db("urlshortener");
const urls = db.collection("urls");

// ... rest of the code

Upon researching this problem, I've discovered that MongoDB Atlas enforces cluster access restrictions that solely allow authorized IP addresses (https://www.mongodb.com/docs/atlas/troubleshoot-connection/). I appreciate the security considerations, but I'm currently grappling with how to ensure the functionality of my app while it's hosted on platforms like Replit or Heroku, thereby enabling it to be accessible online.

For context, here are the links to my Replit project and GitHub repository:

Additionally, when attempting to access MongoDB via my Replit project, I encountered the following log error:

{ url: 'https://github.com/' }
MongoServerSelectionError: connection <monitor> to 3.120.252.84:27017 closed
    at Timeout._onTimeout (/home/runner/boilerplate-project-urlshortener/node_modules/mongodb/lib/sdam/topology.js:278:38)
    at listOnTimeout (node:internal/timers:564:17)
    at process.processTimers (node:internal/timers:507:7) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map(3) {
      'ac-vdgrq2j-shard-00-00.3ckzmha.mongodb.net:27017' => [ServerDescription],
      'ac-vdgrq2j-shard-00-01.3ckzmha.mongodb.net:27017' => [ServerDescription],
      'ac-vdgrq2j-shard-00-02.3ckzmha.mongodb.net:27017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: 'atlas-rlmbn4-shard-0',
    maxElectionId: null,
    maxSetVersion: null,
    commonWireVersion: 0,
    logicalSessionTimeoutMinutes: null
  },
  code: undefined,
  [Symbol(errorLabels)]: Set(0) {}
}

I've reviewed several YouTube tutorials, and it appears that other developers can deploy their applications without encountering analogous MongoDB connection difficulties.

In my previous attempts to troubleshoot this issue, I've sought advice from both ChatGPT and Bard. Despite their recommendations, I've found some of the suggested actions to be somewhat advanced and potentially risky given my current skill level.

One proposal involved altering MongoDB configuration settings, specifically by deselecting the "Allow only connections from the following IP addresses" option and editing the mongod.conf file to remove the line bind_ip=127.0.0.1. Although these steps may prove effective, I'm concerned about unintentionally introducing new vulnerabilities by making such changes.

In the interim, I've taken precautions such as excluding sensitive information from my GitHub repository and utilizing environment variables for confidential data storage. Nonetheless, I'm eager to ensure that my deployment approach adheres to best practices, thereby maintaining functionality and security.

I'm uncertain about the necessary steps to configure my MongoDB connection appropriately for compatibility with these platforms. Any guidance or suggestions on resolving this issue in a beginner-friendly manner would be greatly appreciated.

Reggroy
  • 53
  • 5

0 Answers0