1
votes

I'm using macOs. I have followed the web dev tutorial on https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/development_environment and https://www.youtube.com/watch?v=1NrHkjlWVhM. It is a simple node app with the ff npm installations:

  • express
  • pug
  • mongoose ("^5.9.26")
  • --save-dev nodemon

Note that MongoDB is not installed in my computer as it was not required by the tutorials.

Here is my code:

const mongoose = require('mongoose')

mongoose.connect('mongodb://localhost/blog', {
     useNewUrlParser: true, 
     useUnifiedTopology: true
}).catch(error => console.log(error))

I have tried changing the server address to the ff without success

  • mongodb://localhost:27017/blog
  • mongodb://127.0.0.1:27017/blog
  • mongodb://127.0.0.1/blog

and would continually get the ff error log. How do I get around this?

[nodemon] restarting due to changes...
[nodemon] starting `node server.js`
MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
    at NativeConnection.Connection.openUri (/Users/Dave/Desktop/apps/test-blog/node_modules/mongoose/lib/connection.js:827:32)
    at Mongoose.connect (/Users/Dave/Desktop/apps/test-blog/node_modules/mongoose/lib/index.js:335:15)
    at Object.<anonymous> (/Users/Dave/Desktop/apps/test-blog/server.js:6:10)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47 {
  reason: TopologyDescription {
    type: 'Single',
    setName: null,
    maxSetVersion: null,
    maxElectionId: null,
    servers: Map { 'localhost:27017' => [ServerDescription] },
    stale: false,
    compatible: true,
    compatibilityError: null,
    logicalSessionTimeoutMinutes: null,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    commonWireVersion: null
  }
}
1

1 Answers

0
votes

Note that MongoDB is not installed in my computer as it was not required by the tutorials

The MongoDB URI mongodb://localhost:27017 is pointing to the local computer. In other words, a MongoDB instance must be installed on the local computer, in order for the Node script to connect to one.

Note that in general, the localhost domain and its equivalent IP address 127.0.0.1 are reserved for services running on the local computer. For example, in this case, a local MongoDB instance is expected to run at the localhost domain at the default 27017 port, as indicated in the error message.

MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017