5

I have that code:

var express = require('express'),
    stylus = require('stylus'),
    logger = require('morgan'),
    bodyParser = require('body-parser'),
    mongoose = require('mongoose');

var env = process.env.NODE_ENV = process.env.NODE_ENV || 'development';

var app = express();

function compile(str, path){
    return stylus(str).set('filename', path);
}

app.set('views', __dirname + '/server/views');
app.set('view engine', 'jade');
app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(stylus.middleware(
        {
            src: __dirname + '/public',
            compile: compile
        }
    ));
app.use(express.static(__dirname + '/public'));

mongoose.connect('mongodb://localhost/multivision');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error..'));
db.once('open', function callback(){
    console.log('multivision db opened');
});

app.get('/partials/:partialPath', function(req, res){
    res.render('partials/' + req.params.partialPath);
});

app.get('*', function(req, res) {
  res.render('index');
});

var port = 3131;
app.listen(port);
console.log('Listening on port ' + port + '...');

but when I'm trying nodemon server.js it throws an error:

connection error.. { [MongoError: connect ECONNREFUSED 127.0.0.1:27017] name: 'MongoError' message: 'connect ECONNREFUSED 127.0.0.1:27017' }

how can I improve that? I've already installed mongoose using npm install mongoose --save in my directory

Yeah there are dozens question like this but none of these helped me.. I'm new at nodejs and probably missing something

gsiradze
  • 4,583
  • 15
  • 64
  • 111

14 Answers14

11

I was also facing the same issue, when I was executing node server on my project directory. For me the MongoDB service was not started, that makes this issue.

So I had to run services.msc and activated the service.

enter image description here

After that I was able to run my command.

D:\SVenu\MyApp>node server
Saving User
App is listening on port: 3000
Already Exist
Already Exist
Already Exist
Already Exist
Done save
Sibeesh Venu
  • 18,755
  • 12
  • 103
  • 140
11

run services.msc and activate the Mongodb service.

Now Mongodb will connect

the status of the Mongodb indicated as Running

enter image description here

Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35
Satheesh kumar
  • 111
  • 1
  • 3
5

I was having the same problem, and found that it was a mongod issue.(I am running Ubuntu 16.04). Looking through the errors there was a directory missing. After adding the directory, I needed to change permissions, and finally, set the mongod service to start at boot.

$ sudo mkdir -p /data/db
$ sudo chown -R $USER /data/db
$ sudo systemctl enable mongod.service
froglegs
  • 133
  • 2
  • 8
  • I was having this problem inside a custom Docker container, after adding `chown -R $USER /data/db` it worked! Thanks! – Lucas Abbade May 08 '19 at 03:41
4

Your mongodb service is probably down. Run sudo service mongod start to start the daemon process

Alberto Centelles
  • 1,215
  • 11
  • 24
2

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

Fayez Nazzal
  • 319
  • 2
  • 10
2

If you're in windows and you had this issue, just go the installer exe app for the mongodb and click "Repair" this works for me

Osta
  • 33
  • 6
1

try this:

mongod.exe --dbpath c:\data\db

c:\data\db is the place where you put your db.

and when you see something like this :

2016-08-18T10:22:31.020+0800 I CONTROL  [main] Hotfix KB2731284 or later update is not installed, will zero-out data files
2016-08-18T10:22:31.022+0800 I CONTROL  [initandlisten] MongoDB starting : pid=4356 port=27017 dbpath=c:\data\db 64-bit host=sevencai-PC0
2016-08-18T10:22:31.022+0800 I CONTROL  [initandlisten] targetMinOS: Windows 7/Windows Server 2008 R2
2016-08-18T10:22:31.023+0800 I CONTROL  [initandlisten] db version v3.2.8
2016-08-18T10:22:31.023+0800 I CONTROL  [initandlisten] git version: ed70e33130c977bda0024c125b56d159573dbaf0

......

and then node yourserver.js

maybe everything will be fine!

Seven
  • 549
  • 1
  • 4
  • 10
1

Follow as below:

=> run services.msc and start mondodb server

and save the file in node project, you can see the server connected!!

Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35
  • Please don't post "have same issue" as an answer. Please read the [tour](https://stackoverflow.com/tour), and [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – Tomer Shetah Dec 17 '20 at 12:29
0

enter image description here

In Most of the case you will get the error bcz the service MongoDB Database Server (MongoDB) Might be stopped.

Just start the service to be connected with DB enter image description here

Debendra Dash
  • 5,334
  • 46
  • 38
0

it's doesn't work as localhost but IP address works 127.0.0.1 and solves this problem:

  // .connect("mongodb://localhost:27017/lofydb")

    const mongoose = require("mongoose");

    mongoose.connect("mongodb://127.0.0.1:27017/lofydb")
      // .connect("mongodb://127.0.0.1:27017")
    
      .then(() => {
        console.log("Connected to Database");
      })
      .catch((err) => {
        console.log("Not Connected to Database ERROR! ", err);
      });
bguiz
  • 27,371
  • 47
  • 154
  • 243
0
  1. please check the MongoDB service is active and running. Refer the running services

  2. If it is running and available on MongoDB compass or on MongoDB shell and if you are working with node version >17.0.0 or so it will give error. So plz change to the stable version of node.

Saravanan G
  • 581
  • 1
  • 9
  • 26
0

Make sure MongdoDB is running

To run MongoDB as a macOS service, run:

brew services start mongodb-community@5.0

And when you see:

==> Successfully started mongodb-community (label: homebrew.mxcl.mongodb-commu

OR type on command line, to check the mongo service.

mongod

After run service, You can easily connect with mongdoDB localhost or remote connection.

0

I had the same issue and it had to do with the fact that my MongoDB service wasn't running locally. Make sure you followed all the correct installation steps for whatever OS you are trying to run MongoDB service here: https://www.mongodb.com/docs/manual/administration/install-community/

Then follow the respective guide for the running the service under "Run MongoDB Community Edition". So for example, for macOS you would do

brew services start mongodb-community@6.0

if you wanted to run as a macOS service, or

mongod --config /opt/homebrew/etc/mongod.conf --fork

if you wanted to run manually as a background service on Apple M1 processor. The same page I shared above also has commands you can run for verifying if your MongoDB process is running.

0

Step 1: open conf file to edit.

sudo vim /etc/mongod.conf

Step 2: find port and change it.

net: port: 27017 -> default port before any change

After changing port, my issue solved :