繁体   English   中英

无法连接到MongoDB。 请确保mongod在mongodb:// localhost:27017 / mongo-server上运行

[英]Unable to connect to MongoDB. Please make sure mongod is running on mongodb://localhost:27017/mongo-server

运行index.js时遇到此问题。

这是我的代码:

var http = require('http'),
    express = require('express'),
    path = require('path'),
    MongoClient = require('mongodb').MongoClient,
    Server = require('mongodb').Server,
    CollectionDriver = require('./collectionDriver').CollectionDriver;

var app = express();
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: false}));

var mongoHost = 'localHost';
var mongoPort = 27017;
var collectionDriver;

var url = 'mongodb://localhost:27017/mongo-server';
// Use connect method to connect to the server
MongoClient.connect(url, function(error, db) {
  if (error) {
    console.error("Unable to connect to MongoDB. Please make sure mongod is running on %s.", url);
    process.exit(1);
  }

  console.log("Connected to MongoDB successfully.");
  collectionDriver = new CollectionDriver(db);
});

app.use(express.static(path.join(__dirname, 'public')));

app.get('/', function (req, res) {
  res.send('<html><body><h1>Hello World</h1></body></html>');
});

app.get('/:collection', function(req, res) {
   var params = req.params;
   collectionDriver.findAll(req.params.collection, function(error, objs) {
          if (error) { res.send(400, error); }
          else {
              if (req.accepts('html')) {
                  res.render('data',{objects: objs, collection: req.params.collection});
              } else {
              res.set('Content-Type','application/json');
                  res.send(200, objs);
              }
         }
    });
});

app.get('/:collection/:entity', function(req, res) {
   var params = req.params;
   var entity = params.entity;
   var collection = params.collection;
   if (entity) {
       collectionDriver.get(collection, entity, function(error, objs) {
          if (error) { res.send(400, error); }
          else { res.send(200, objs); }
       });
   } else {
      res.send(400, {error: 'bad url', url: req.url});
   }
});

app.post('/:collection', function(req, res) {
    var object = req.body;
    var collection = req.params.collection;
    collectionDriver.save(collection, object, function(err,docs) {
          if (err) { res.send(400, err); }
          else { res.send(201, docs); }
     });
});

app.put('/:collection/:entity', function(req, res) {
    var params = req.params;
    var entity = params.entity;
    var collection = params.collection;
    if (entity) {
       collectionDriver.update(collection, req.body, entity, function(error, objs) {
          if (error) { res.send(400, error); }
          else { res.send(200, objs); }
       });
   } else {
       var error = { "message" : "Cannot PUT a whole collection" }
       res.send(400, error);
   }
});

app.delete('/:collection/:entity', function(req, res) {
    var params = req.params;
    var entity = params.entity;
    var collection = params.collection;
    if (entity) {
       collectionDriver.delete(collection, entity, function(error, objs) {
          if (error) { res.send(400, error); }
          else { res.send(200, objs); }
       });
   } else {
       var error = { "message" : "Cannot DELETE a whole collection" }
       res.send(400, error);
   }
});

app.use(function (req,res) {
    res.render('404', {url:req.url});
});

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

我是MongoDB的新手。

  • 为此,首先启动Mongo Server步骤
  • C:\\ Program Files \\ MongoDB \\ Server \\ 3.2 \\ bin
  • 现在写mongod
  • 成功运行后,您将看到此输出

我的连接代码

var MongoClient = require('mongodb').MongoClient,
    format = require('util').format;
MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db) {
    if (err) {
        throw err;
    } else {
        console.log("successfully connected to the database");
    }
    db.close();
});

在此处输入图片说明

确保从终端或命令提示符启动mongo服务器。

请按照以下步骤启动服务器:

  1. 如果未安装mongodb,请从https://www.mongodb.com/download-center#community安装。

  2. 您必须在c文件夹中创建一个新目录,名称为data,内部数据再创建一个名为db的目录。 这些文件夹是您的mongodb保存数据所必需的。

  3. 现在打开终端或命令提示符,在位于mongodb文件夹中的bin文件夹中导航,然后输入mongod。 这将启动您的服务器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM