我在Cloud9中的连接数据库MongoDB有问题,请帮助解决这个问题!
var MongoClient = require("mongodb").MongoClient;
var port = process.env.PORT;
var ip = process.env.IP;
MongoClient.connect("mongodb://"+ip+":"+port+"/test",function(error,db){
if(!error){
console.log("We are connected");
}
else{
console.dir(error); //failed to connect to [127.4.68.129:8080]
}
});输出:
Running Node Process
Your code is running at 'http://demo-project.alfared1991.c9.io'.
Important: use 'process.env.PORT' as the port and 'process.env.IP' as the host in your scripts!
[Error: failed to connect to [127.4.68.129:8080]]发布于 2013-01-21 17:41:25
process.env.PORT和process.env.IP是应用程序的端口和IP地址,而不是数据库。您需要从MongoDB提供程序中提取Mongo连接字符串。
下面是修改为使用这两个环境变量的Node.js homepage中的hello world示例。
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(process.env.PORT || 1337, process.env.IP || '127.0.0.1');发布于 2015-08-22 20:51:23
如果您使用https://docs.c9.io/setting_up_mongodb.html这个链接,您将在您的工作区中设置并运行您的mongodb守护进程。
如果你看一下./mongod的输出,你会发现这个输出:
2015-08-22T12:46:47.120+0000 [initandlisten] MongoDB starting : pid=7699 port=27017 dbpath=data 64-bit host=velvetdeth-express-example-1804858只需将主机和端口值复制到mongodb配置中,设置数据库url,在本例中为:
mongodb://velvetdeth-express-example-1804858:27017发布于 2014-07-26 00:18:38
对于遇到这个问题的其他人,解决方案在这里:https://docs.c9.io/setting_up_mongodb.html
Cloud9工作区中预安装了MongoDB。运行以下命令:
$ mkdir data
$ echo 'mongod --bind_ip=$IP --dbpath=data --nojournal --rest "$@"' > mongod
$ chmod a+x mongod要启动Mongodb进程,请运行:
$ ./mongod然后“运行”你的node.js应用脚本,你就可以开始比赛了。
下面是这些参数的含义:
--dbpath=data (因为它缺省为不可访问的/var/db )
--nojournal,因为mongodb通常会预先分配2 GB的日志文件(超过Cloud9磁盘空间配额)
-- bind _ip=$IP (因为不能绑定到0.0.0.0)
--rest在默认端口28017上运行
https://stackoverflow.com/questions/14435143
复制相似问题