Corey Lapka
Corey Lapka

Reputation: 21

mongodb 4.0.1 ECONNREFUSED Works in node.js on dev computer

I have a personal AWS server running MongoDB 4.0.1 and Docker version 17.03.2-ce, build f5ec1e2 I just got the latest version of node.js (10.9)

I can connect, view and edit documents on MongoDB using Compass.

When I run my node.js on my dev computer, I can use postman to get and post. When I build a docker container and run it on the AWS docker postman returns ECONNREFUSED.

//index.js
const express = require('express');
const cors = require('cors');

// set up express app
const app = express();
app.use(cors());
app.use(express.json());

//initialize routes  - set router to 
app.use('/database', require('./routes/recipes'));    

// listen for requests
const PORT = 4000;
const HOST = '0.0.0.0';
app.listen(PORT, HOST, function () {
    console.log('now listening for requests');
});


//recipes.js
const express = require('express');
const router = express.Router();

const MongoClient = require('mongodb');
const url = 'mongodb://localhost:27017';
//const url = 'mongodb://localhost/local';

// get a list of recipes
router.get('/recipes', function (req, res) {
...

Upvotes: 1

Views: 71

Answers (1)

Corey Lapka
Corey Lapka

Reputation: 21

replace localhost with the AWS PRIVATE IP address, not the public one or the public url.

const url = 'mongodb://localhost:27017';

Upvotes: 1

Related Questions