Connecting mongodb to nodejs

  1. Restarted MongoDB server (it keeps running when error occurs).

  2. Using MongoDB server on windows as a service (started it manually)
    Established the connection via MongoDB Shell CLI Package by hitting enter in the comand prompt to establish the default connection.

  3. (mongodb://127.0.0.1:27017/directConnection=true&serverSelectionTimeoutMS=2000 )
    Called npm install and npm start (my dependencies are listed below)

  4. Checked that MongoDB is running

  5. Checked via the windows resource monitor that the port 27017 is occupied by mongod.exe using TCP and is not restricted by the firewall

  6. Checked that I am not using a VPN nor a proxy connection that could interfere.

  7. Then I opened http://localhost:3000/ to which I am listening (app.listen(3000);)

Why am i still getting this error even after trying all of the above steps?


MongoServerSelectionError: connect ECONNREFUSED ::1:27017
    at Timeout._onTimeout (C:UsersridwaanDocumentsweb devtestnode_modulesmongodblibsdamtopology.js:330:38)
    at listOnTimeout (node:internal/timers:557:17)
    at processTimers (node:internal/timers:500:7) {
  reason: TopologyDescription {
    type: 'Unknown',
    servers: Map(1) {
      'localhost:27017' => ServerDescription {
        _hostAddress: HostAddress { isIPv6: false, host: 'localhost', port: 27017 },
        address: 'localhost:27017',
        type: 'Unknown',
        hosts: [],
        passives: [],
        arbiters: [],
        tags: {},
        minWireVersion: 0,
        maxWireVersion: 0,
        roundTripTime: -1,
        lastUpdateTime: 1717487,
        lastWriteDate: 0,
        error: MongoNetworkError: connect ECONNREFUSED ::1:27017
            at connectionFailureError (C:UsersridwaanDocumentsweb devtestnode_modulesmongodblibcmapconnect.js:293:20)
            at Socket.<anonymous> (C:UsersridwaanDocumentsweb devtestnode_modulesmongodblibcmapconnect.js:267:22)
            at Object.onceWrapper (node:events:510:26)
            at Socket.emit (node:events:390:28)
            at emitErrorNT (node:internal/streams/destroy:164:8)
            at emitErrorCloseNT (node:internal/streams/destroy:129:3)
            at processTicksAndRejections (node:internal/process/task_queues:83:21)
      }
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    logicalSessionTimeoutMinutes: undefined
  }
}

Node.js v17.1.0