javascript - mongodb 和 node.js 驱动程序未连接聊天休息 api

标签 javascript node.js mongodb rest node.js-stream

在使用 node.js/mongodb 核心模块尝试连接到 mongodb 时出现错误。

当前使用node.js v0.10.25和mongodb v2.6.1

我的错误之一是连接到 mongodb

mongo.Db.connect(host, function(error, client) {                                                                                               
         ^                                                                                                                                     
TypeError: Object function (databaseName, topology, options) {

运行nodemon时我也会得到这个

[nodemon] app crashed - waiting for file changes before starting...

这是我的聊天休息 API 代码

// first we include our libraries
var http = require('http');
var util = require('util');
var querystring = require('querystring');
var mongo = require('mongodb');

// make a string to connect to MongoDB:
var host = "mongodb://localhost/test";

// We put all the logic inside of an open connection in the form of a callback function:
mongo.Db.connect(host, function(error, client) {
    console.log("this is working");
    if (error) throw error;

    var collection = new mongo.Collection(client, 'messages');

    var app = http.createServer( function (request, response) {

        if (request.method === "GET" && request.url === "/messages/list.json") {
            collection.find().toArray(function(error,results) {
                response.writeHead(200,{'Content-Type':'text/plain'});
                console.dir(results);
                response.end(JSON.stringify(results));
            });
        };

        if (request.method === "POST" && request.url === "/messages/create.json") {
            request.on('data', function(data) {
                collection.insert(querystring.parse(data.toString('utf-8')), {safe:true}, function(error, obj) {
                    if (error) throw error;
                    response.end(JSON.stringify(obj));
            })              
        })
    };
});

    var port = process.env.PORT || 3000;
    app.listen(port);
})

希望有人能帮助我:)

最佳答案

问题是建立与 mongodb 驱动程序的连接。当前使用的方法适用于 mongodb v1.49,此后已随当前版本的 mongodb v2.6.1 进行更改。

以下是更新后的代码,反射(reflect)连接到当前版本的 mongodb 所需的更改。

    /*
     * github https://github.com/azat-co/rpjs/tree/master/mongo
     *  
     * To test via CURL terminal commands and see list of messages run: 
     * $ curl http://localhost:3000/messages/list.json 
     * 
     * NOTE: Will return empty array
     * 
     * POST a new message:
     * $ curl -d "username=BOB&message=test" http://localhost:3000/messages/create.json 
     * 
     * To add new messages:
     * $ curl -X POST -d 'name=sosana&message=waz up' http://localhost:3000/messages/create.json 
     * 
     * To check if messages worked
     * $ curl localhost:3000/messages/list.json
     * 
    */

    // first we include our libraries
    var http = require('http');
    var util = require('util');
    var querystring = require('querystring');
    var MongoClient = require('mongodb').MongoClient;
    var mongo = new MongoClient();

    // make a string to connect to MongoDB:
    var host = "mongodb://localhost/test";

    // We put all the logic inside of an open connection in the form of a callback function:
    mongo.connect(host, function(error, db) {
        console.log("this is working");
        if (error) throw error;

        // establish a connection to the database
        var myDB = db.db("test");

        // establish a connection to the collection 
        var collection =  myDB.collection("messages");

        var app = http.createServer( function (request, response) {

            if (request.method === "GET" && request.url === "/messages/list.json") {
                collection.find().toArray(function(error,results) {
                    response.writeHead(200,{'Content-Type':'text/plain'});
                    console.dir(results);
                    response.end(JSON.stringify(results));
                });
            };

            if (request.method === "POST" && request.url === "/messages/create.json") {
                request.on('data', function(data) {
                    collection.insert(querystring.parse(data.toString('utf-8')), {safe:true}, function(error, obj) {
                        if (error) throw error;
                        response.end(JSON.stringify(obj));
                })              
            })
        };
});

    var port = process.env.PORT || 3000;
    app.listen(port);
    console.log("listening on port " + port);
})

关于javascript - mongodb 和 node.js 驱动程序未连接聊天休息 api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30427536/

相关文章:

javascript - Angular 2 Material如何设置一个字段不变

javascript - 在 chrome 新版本上使用 window.open 显示 base64 pdf 数据

javascript - readFile html参数在 Node 中返回null

node.js - 从 Node/Express 中的另一个路由中调用 API 端点

C++ 数据库持久化

mongodb - 如何在mongodb中返回弹出的元素

javascript - PhpStorm 不会使用 ES6 模板文字进行编译

javascript - 通过 CDN 使用 Dojo 时如何加载自定义 AMD 模块?

node.js - docker-compose up 找不到模块,但从 bash 运行有效

mongodb - 是否有 MongoDB 图形日志查看器?