1

I am trying to build a node.js API using Express.js and MongoDB I have called the libraries and MongoDB, also I have set a port and MongoDB connection.

When I execute the command npm start. The server starts but it still gives an error.

const express = require('express');
const app = express();
const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/subscribers', {useNewUrlParser: true, useUnifiedTopology: true})
    .then(() => console.log('MongoDB Connected...'))
    .catch((err) => console.log(err))

app.listen(3000, () => console.log('server started'));
zhulien
  • 5,145
  • 3
  • 22
  • 36
  • Does this answer your question? [MongoNetworkError: failed to connect to server \[localhost:27017\] on first connect \[MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017\]](https://stackoverflow.com/questions/50173080/mongonetworkerror-failed-to-connect-to-server-localhost27017-on-first-connec) – rohanraj Jun 17 '21 at 06:31

3 Answers3

0

I know this issue is old, but i came across a similar issue and the above solutions did not work for me, I'm using Ubuntu 20.04 LTS.

What i did to make it work was just running mongo service using this command:

$ mongod

Then everything worked fine

0

If you already installed "MongoDB", if you accidentally exit from the MongoDB server, then "restart your system".

And also another method is there:

press:

Windows + R

type:

services.msc

and click "ok", it opens "services" window, and then search for "MongoDB Server" in the list. After you find "MongoDB Server", right-click and choose "start" from the pop-up menu.

The MongoDb Server will start running.

0

go into this directory in your file explorer

C:\Program Files\MongoDB\Server\4.4\bin

then hold Shift key and press mouse Right button

then open powershell window

and type mongod this will start your mongo server then try Running your code.

[see the pic it will work]

https://i.stack.imgur.com/35nEn.png