Meghan King
Meghan King

Reputation: 215

MongoDB net::ERR_CONNECTION_REFUSED on server

I am trying to make calls to my database from my react/node app hosted on a server. When run locally (using nodemon) the application is running at localhost:3000 and the server is hosted on port 4000, so I make api calls to localhost:4000. For example, localhost:4000/students displays the list of students stored in the MongoDB.

My server.js is as follows:

const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const PORT = 4000;
const cors = require('cors');
const mongoose = require('mongoose');
const studentRoute = require('./student.route');
const config = require('./secrets');

mongoose.Promise = global.Promise;
mongoose.connect(config.dbUri, { useNewUrlParser: true }).then(
  () => {console.log('Database is connected') },
  err => { console.log('Can not connect to the database'+ err)}
);

I am deploying my application to my site by uploading the build folder that results from npm run build to a folder called 'the-wall' (the name of the project). So to access the application I go to example.com/the-wall.

My mongoDB is hosted using MongoDB atlas, and have whitelisted my IP address and what I believe to be the IP address of the server. I am making calls using axios, with the following config:


const env = process.env.NODE_ENV; 

export const app = axios.create({
  baseURL:
    env === 'production'
      ? 'https://example.com:4000/'
      : 'http://localhost:4000/',
});

However, when I try to access example.com:4000/student I receive a net::ERR_CONNECTION_REFUSED error. As far as I can tell, mongoDB is not installed on the server. Is the URL I am using incorrect, or is there additional set up I need to do?

Note: I have also tried example.com/the-wall:4000/student, but I get a 404 error with this one.

Does the way I am trying to make a call to the database look correct? (i.e. is the URL correct with the port, etc)

Upvotes: 0

Views: 4549

Answers (3)

I had the same problem, on localhost I could access mongodb atlas, when I accessed it on another computer it gave me this error, I was able to solve it by changing the axios url, instead of leaving localhost I gave the IP of the computer that is running the server type http:/ /localhost:3000 moved to http:192.168.1.2:3000. That resolved it.

Upvotes: 0

Hudu
Hudu

Reputation: 31

This usually occurs when there is a change in ip address.

check your connection on mongoDB, go to cluster and choose the project you're working on, navigate to network access and update your current ip address, right on top, find database access and update users by editing users to update it accordingly. check this https://www.mongodb.com/docs/atlas/troubleshoot-connection/

This should work for you.

Upvotes: 0

farvilain
farvilain

Reputation: 2562

Try to make it works locally with the production database.

If it works, it's an IP configuration problem on your server.

If it fails, it's a code configuration problem.

Upvotes: 1

Related Questions