在Ubuntu中的node.js中使用Express,MongoDB和Mongoose的REST API。 MongoError:首次连接

时间:2017-05-15 00:27:57

标签: node.js mongodb express mongoose

在运行我的快速应用程序时,我遇到了以下错误:

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]' }

最初我在连接MongoDB服务器时遇到了问题,但我已经通过终端中的以下命令终止所有进程来解决:

  1. sudo killall mongod
  2. 确保目录设置在正确的位置,以便Mongo可以找到它:

    1. sudo mkdir -p / data / db /
    2. sudo chown id -u / data / db
    3. 现在我的MongoDB服务器启动并运行:

      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
      

      但在通过terminal by command运行我的快速应用程序时仍然遇到同样的问题:     npm start

      这是我的代码:

      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;
      

2 个答案:

答案 0 :(得分:4)

我看到你mongod中有port = 27017,但代码中有27012。 改变

  var url = 'mongodb://localhost:27012/conFusion';

   var url = 'mongodb://localhost:27017/conFusion';

答案 1 :(得分:0)

您正在使用默认的mongDB端口,即27017.确保运行该命令并找到默认端口号

  

sudo lsof -iTCP -sTCP:LISTEN | grep mongo

输出可能是:localhost:27017(Listen)。 如果端口号为27017,则将端口号27012更改为27017。

更改

var url = 'mongodb://localhost:27012/conFusion';

var url = 'mongodb://localhost:27017/conFusion';

,否则

如果要将默认端口从27017更改为27012,则

  

sudo gedit /etc/mongod.conf

将端口号更改为27012,您与代码无关。 您可以查看mongoDB port number