javascript - 如何使用 node-mongodb-native 连接到 Modulus.io?

标签 javascript node.js mongodb

这里是第一个问题,所以请友善;)

我正在配置 Node.js 服务器以连接到 Modulus.io node.js 托管中的 MongoDB 数据库(非常好的东西,值得一试),但我似乎无法正确建立连接。 Per the getting-started guide我得到一个连接 uri,格式如下:

mongodb://user:pass@mongo.onmodulus.net:27017/3xam913

但这似乎不适用于我尝试移植到服务器(在本地运行)的代码结构,因为 Server class仅需要定义主机和端口的参数结构...

这是我试图适应连接的代码:

// server setup
var mongo = require('mongodb'),
    mdbServer = mongo.Server,
    mdbDb = mongo.Db,
    mdbObjectID = mongo.ObjectID;

// open a connection to the mongoDB server
var mdbserver = new mdbServer('localhost', 27017, {auto_reconnect: true});

// request or create a database called "spots03"
var db = new mdbDb('spots03', mdbserver, {safe: true});

// global var that will hold the spots collection
var spotsCol = null;

// open the database
db.open(function(err, db) {
    if(!err) {
        // if all cool
        console.log("Database connection successful");

        // open (get/create) a collection named spotsCollection, and if 200, 
        // point it to the global spotsCol
        db.createCollection(
            'spotsCollection',
            {safe: false},  // if col exists, get the existing one
            function(err, collection) {spotsCol = collection;}
        );
    }
});

任何帮助将不胜感激,谢谢!

最佳答案

看起来有几件事:

  1. 连接 URL 应为 mongo.onmodulus.net

    var mdbserver = new mdbServer('mongo.onmodulus.net', 27017, {auto_reconnect: true});

  2. rounce正确,数据库名称是Modulus自动生成的。

    var db = new mdbDb('3xam913', mdbserver, {safe: true});

  3. Modulus 数据库需要身份验证。在调用 createCollection 之前,您必须调用 auth 并向其传递在项目仪表板上设置的用户凭据。

我是一名 Modulus 开发人员,我知道数据库名称并不理想。

编辑:这是一个工作示例的完整源代码。它记录每个 HTTP 请求,然后将所有请求发送回用户。

var express = require('express'),
      mongo = require('mongodb'),
     Server = mongo.Server,
         Db = mongo.Db;

var app = express();

var server = new Server('mongo.onmodulus.net', 27017, { auto_reconnect: true });
var client = new Db('piri3niR', server, { w: 0 });
client.open(function(err, result) {
  client.authenticate('MyUser', 'MyPass', function(err, result) {
    if(!err) {
      console.log('Mongo Authenticated. Starting Server on port ' + (process.env.PORT || 8080));
      app.listen(process.env.PORT || 8080);
    }
    else {
      console.log(err);
    }
  });
});

app.get('/*', function(req, res) {
  client.collection('hits', function(err, collection) {
    collection.save({ hit: req.url });

    // Wait a second then print all hits.
    setTimeout(function() {
      collection.find(function(err, cursor) {
        cursor.toArray(function(err, results) {
          res.send(results);
        });
      });
    }, 1000)
  });
});

关于javascript - 如何使用 node-mongodb-native 连接到 Modulus.io?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14072603/

相关文章:

javascript - 可以在 nodeJS 中创建 3D 多人游戏服务器

python - 克隆文档 mongoengine

MySQL 与 MongoDB 1000 次读取

java - 使用外部 Java 插件在 PhoneGap/Cordova 中捕获音频/视频

javascript - ESlint - import.meta 导致致命解析错误

javascript - 如何使用react发送表单数据

javascript - 如何更改 Jasmine 中包含的 Javascript 文件中定义的全局变量?

xml - 存储 XML 文件的良好实践 - Express、MongoDB

javascript - 如何在 HTML 中显示字符串 javascript

javascript - ember-dev-fixtures 错误 : "Assertion Failed: The response from a findAll must be an Array, not undefined"