Mongodb和node.js驱动程序不连接聊天rest api
mongodb and node.js drivers not connecting for chat rest api
当尝试使用node.js/mongodb核心模块连接到mongodb时,我得到错误。
当前使用node.js v0.10.25和mongodb v2.6.1
我的一个错误是连接到mongodbmongo.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的当前版本。
下面是更新后的代码,以反映连接到当前版本的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);
})
相关文章:
- Amazon S3 REST API大小不正确
- 客户端服务器REST API captcha实现
- Ajax聊天消息重复而不仅仅是更新
- 在不打开聊天屏幕的情况下制作Zopim-ding代理
- WebRTC视频聊天可以在FF中使用,但不能在Chrome中使用
- 角度p2p视频聊天-远程流是黑视频
- SharePoint 2010 Jquery/JSON Rest Multiple OR语句-如何
- 使用angularjs中的rest调用通过id获取数据
- 在同一ip但不同端口上使用Rest API
- REST的面向公众的身份验证机制
- 为什么使用AWS Signature v4对S3的REST调用并没有真正使用签名过程
- Nodejs私有消息/w”;在聊天中
- PHP REST服务抛出异常——希望避免控制台错误
- 从ANGULAL连接到REST API
- 从REST服务器和Coffeescapet前端开始
- 如何在使用loopback REST API时更改gulp-angular项目的端口号
- Cross domain - REST API
- XHR不适用于聊天应用程序
- 我正在创建一个聊天,但每次我发送消息时,它都不会让我再发送另一条消息,就像表格一样;不起作用
- Mongodb和node.js驱动程序不连接聊天rest api