0

I am new to mongodb and node.js. I got below error when I was trying to run node.js application, the error log seems mongodb instance refused my request:

 Error: connect ECONNREFUSED 127.0.0.1:27017
    at Object.exports._errnoException (util.js:856:11)
    at exports._exceptionWithHostPort (util.js:879:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1053:14)
 Press any key to continue...

Here is the code of app.js which I used to connect mongodb :

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

// Create a reference to mongoose
var mongoose = require('mongoose');


var routes = require('./routes/index');
var users = require('./routes/users');

var app = express();

// Open a connection to the DB. Here bookAPI is the name of database.
var db = mongoose.connect('mongodb://127.0.0.1:27017/bookAPI');

// Create a reference to models
var Book = require('./models/bookModel');

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(require('stylus').middleware(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function (req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function (err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});

module.exports = app;
1
  • Are you absolutely sure mongodb is running? Try 'mongodb://localhost /bookAPI' Commented Apr 23, 2016 at 4:23

3 Answers 3

1

There is no mongodb service listen on 127.0.0.1:27017.
You should start mongodb service first or change your config to connect an existing mongodb service.

Sign up to request clarification or add additional context in comments.

3 Comments

this is for remote connections.
@MarcosR.Guevara, localhost is as same as '127.0.0.1'. In your system configuration '/etc/hosts' , there is a line '127.0.0.1 localhost', so they are equal. They are the address of your local loopback interface.
i now that 127.0.0.1 is localhost but i don't need to call the port (default 27017) for local connections. mongoosejs.com/docs/connections.html
1
mongoose.connect('mongodb://localhost/bookAPI');

Indeed, the service (example: mongod) should be active.

Ensure that your mongodb service is running.

3 Comments

Could you please add some context? What is wrong? What does this code fix?
This code has no difference from mongoose.connect('mongodb://127.0.0.1:27017/bookAPI')
It's working now. My mongodb service was not running. I started it and worked fine. Thank you all.
0

Try this:

mongoose.connect("mongodb://0.0.0.0:27017/bookAPI")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.