Sunday, 14 May 2017

REST API with Express, MongoDB and Mongoose in node.js in Ubuntu. MongoError: failed to connect to server [127.0.0.1:27017] on first connect

While running my express application, I encountered with the following error:

bharti@bharti-Inspiron-3542:/media/bharti/New Volume1/Nodejs/rest-server$ npm start

> rest-server@0.0.0 start /media/bharti/New Volume1/Nodejs/rest-server
> node ./bin/www

connection error: { MongoError: failed to connect to server [localhost:27012] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27012]
    at .<anonymous> (/media/bharti/New Volume1/Nodejs/rest-server/node_modules/mongodb-core/lib/topologies/server.js:328:35)
    at emitOne (events.js:96:13)
    at emit (events.js:188:7)
    at .<anonymous> (/media/bharti/New Volume1/Nodejs/rest-server/node_modules/mongodb-core/lib/connection/pool.js:274:12)
    at g (events.js:286:16)
    at emitTwo (events.js:106:13)
    at emit (events.js:191:7)
    at Socket.<anonymous> (/media/bharti/New Volume1/Nodejs/rest-server/node_modules/mongodb-core/lib/connection/connection.js:177:49)
    at Socket.g (events.js:286:16)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at emitErrorNT (net.js:1271:8)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)
  name: 'MongoError',
  message: 'failed to connect to server [localhost:27012] on first connect [MongoError: connect ECONNREFUSED 127.0.0.1:27012]' }

Initially I had problem in connecting MongoDB server but I have solved by killing all the processes by the following commands in terminal:

  1. sudo killall mongod

to ensure that the directory is set up in the right place so that Mongo can find it:

  1. sudo mkdir -p /data/db/
  2. sudo chown id -u /data/db

Now my MongoDB server is up and running:

bharti@bharti-Inspiron-3542:/media/bharti/New Volume1/Nodejs/mongodb$ mongod --dbpath=data
2017-05-15T05:40:18.099+0530 [initandlisten] MongoDB starting : pid=5170 port=27017 dbpath=data 64-bit host=bharti-Inspiron-3542
2017-05-15T05:40:18.100+0530 [initandlisten] db version v2.6.10
2017-05-15T05:40:18.100+0530 [initandlisten] git version: nogitversion
2017-05-15T05:40:18.100+0530 [initandlisten] OpenSSL version: OpenSSL 1.0.2d 9 Jul 2015
2017-05-15T05:40:18.100+0530 [initandlisten] build info: Linux lgw01-12 3.19.0-25-generic #26~14.04.1-Ubuntu SMP Fri Jul 24 21:16:20 UTC 2015 x86_64 BOOST_LIB_VERSION=1_58
2017-05-15T05:40:18.100+0530 [initandlisten] allocator: tcmalloc
2017-05-15T05:40:18.100+0530 [initandlisten] options: { storage: { dbPath: "data" } }
2017-05-15T05:40:18.134+0530 [initandlisten] journal dir=data/journal
2017-05-15T05:40:18.134+0530 [initandlisten] recover : no journal files present, no recovery needed
2017-05-15T05:40:18.263+0530 [initandlisten] waiting for connections on port 27017

but still have the same problem while running my express application through terminal by command: npm start

and here is my code:

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');
var mongoose = require('mongoose');

var url = 'mongodb://localhost:27012/conFusion';
mongoose.connect(url);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function (){
    //we are connected!
    console.log("Connected correctly to server");
});

var routers = require('./routes/index');
var users = require('./routes/users');
var dishRouter = require('./routes/dishRouter');
var promoRouter = require('./routes/promoRouter');
var leaderRouter = require('./routes/leaderRouter');


var app = express();

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

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

app.use('/', routers);
app.use('/users', users);
app.use('/dishes', dishRouter);
app.use('/promotions', promoRouter);
app.use('/leadership', leaderRouter);

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


//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
        });
    });
}


app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  // res.locals.message = err.message;
  // res.locals.error = req.app.get('env') === 'development' ? err : {};

  res.status(err.status || 500);
  res.render('error', {
    message: err.message,
    error: err
  });
});

module.exports = app;



via Bharti Sharma

No comments:

Post a Comment