MongoDB-Mongopop
MongoDB-Mongopop copied to clipboard
Error: connect ECONNREFUSED 127.0.0.1:27017
I'm working my way through The Modern Application Stack – Part 3: Building a REST API Using Express.js and all is good until I try to actually access the database.
My IP 73.149.42.228 has been whitelisted in MongoDB Atlas.
Running:
curl -g -X POST --data '{
"MongoDBURI": "mongodb://localhost:27017/mongopop?authSource=admin&socketTimeoutMS=30000&maxPoolSize=20",
"collectionName": "simples"}' -i "localhost:3000/pop/countDocs" --header "Content-Type:application/json"
produces the following:
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 325
ETag: W/"145-bNxKpZb5XY3Hu1HWlj+R614UQw8"
Date: Tue, 22 Oct 2019 23:16:30 GMT
Connection: keep-alive
{"success":false,"count":0,"error":"Failed to count the documents: failed to connect to server [localhost:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017\n at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1126:14) {\n name: 'MongoError',\n message: 'connect ECONNREFUSED 127.0.0.1:27017'\n}]"}
Here's the contents of my config.js file:
var config = {
expressPort: 3000,
client: {
mongodb: {
defaultDatabase: "mongopop",
defaultCollection: "simples",
defaultUri: "mongodb://localhost:27017"
},
mockarooUrl: "https://api.mockaroo.com/api/4b1dceb0?count=1000&key=4b0115c0"
},
makerMongoDBURI: "mongodb://localhost:27017/maker?authSource=admin",
checkinCollection: "foursq"
};
module.exports = config;
Hi Dave, you mention Atlas but your MongoDB URI is pointing to localhost. If you click on the "connect" button against your cluster in the Atlas UI then it will give you ad NodeJS URI to copy and use.