您的位置:首页 > 数据库 > Mongodb

部署nodejs mongodb到cloud foundry

2013-10-12 10:37 806 查看
接上一个连接,登录成功之后,切换目录 push app 到cloud
参照官网http://docs.cloudfoundry.com/docs/using/deploying-apps/javascript/#start,但是push总是失败,原因就是没有初始化manifest.yml文件。
第一次创建的过程中,会问你是否save setting,如果自己app中没有此文件的话,一定要输入y。要么你就手动给app 添加此文件。如下图:
cf push 如下图:



push了两天都没push上去,突然看到这个界面还是很欣喜的!!
不过最后,还是没有成功,如下图:




于是查看log,主要的问题点如下:



可以看出是连接db的时候报错了,应该是url路径问题。找到原因了,是启动时连接数据库报错。还是很欣慰的,应该是部署成功了,运行报错!!

喝口水,再继续。。
终于解决了,要命啊!

push 了三天,终于解决问题了。原因有二。1.官网的api不是最新的,服务升级到V2了,但是api没有升级,不知道为什么。2.毕竟是初学,所以像数据库连接等方法,没有来得及研究源码,不知道怎样灵活应对。

好了先看官网,http://docs.cloudfoundry.com/docs/using/services/node-service-bindings.html
先要创建一个服务:
$ cf create-service
然后,修改package.json,添加cf的依赖

"cf-autoconfig":"*"
然后,在需要数据库连接的地方,加上cf的引用

require("cf-autoconfig");
修改监听端口:

app.listen(process.env.VCAP_APP_PORT||3000);
连接数据库服务:

var record_visit = function(req, res){
require('mongodb').connect('', function(err, conn){
conn.collection('ips', function(err, coll){
object_to_insert = { 'ip': req.connection.remoteAddress, 'ts': new Date() };
coll.insert( object_to_insert, {safe:true}, function(err){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(JSON.stringify(object_to_insert));
res.end('\n');
});
});
});
}
按照官网的做法下来,还是会出现上述异常的!!!
官方的API都不对,那才折磨人呢!!幸亏,只是玩儿,慢慢搞吧。

看看官方的解释:http://support.cloudfoundry.com/entries/24374128-Mongodb-Migration-URL-must-be-in-the-format-mongodb-user-pass-host-port-dbname

Hi, apologies for this, we are currently working on bringing the documentation up to date. It's clear that the cf-autoconfig module is not working as it should.

看到这句话,不知道同学们有何感想~

其实这里已经解释的很清楚了,我再说说我解决的过程
1. 首先声明,require('mongodb').open、close的数据库连接方式,cf是不支持的,并且官方已经不支持了,参照https://github.com/mongodb/node-mongodb-native
2.数据库连接的推荐方式:

MongoClient.connect('mongodb://127.0.0.1:27017/test', function(err, db) {
if(err) throw err;
var collection = db.collection('test_insert');
collection.insert({a:2}, function(err, docs) {
collection.count(function(err, count) {
console.log(format("count = %s", count));
});
// Locate all the entries using find
collection.find().toArray(function(err, results) {
console.dir(results);
// Let's close the db
db.close();
});
});
})
3. cf中mongodb服务的url:

var svcs = JSON.parse(process.env.VCAP_SERVICES);
var mongourl = svcs['mongolab-n/a'][0].credentials.uri;
4. 所以,我们用127.0.0.1/db是不能连接数据库的,这也是报错的原因。所以把把URL改为

if(process.env.VCAP_SERVICES){
//app is running in the cloud
var svcs = JSON.parse(process.env.VCAP_SERVICES);
mongourl = svcs['mongolab-n/a'][0].credentials.uri;
}else{
//running locally or not on cloud foundry
mongourl = "mongodb://" + obj.hostname + ":" + obj.port + "/" + obj.db;
}
5.修改所有的数据库连接,如下面形式

var record_visit = function(req, res){
require('mongodb').connect(mongoURL, function(err, conn){
conn.collection('ips', function(err, coll){
object_to_insert = { 'ip': req.connection.remoteAddress, 'ts': new Date() };
coll.insert( object_to_insert, {safe:true}, function(err){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(JSON.stringify(object_to_insert));
res.end('\n');
});
});
});
}
6. 创建一个单独的service
cf create-service
7. 绑定服务到app

cf bind-service --app appName --service serviceName


8. cf push 终于见到素未谋面的提示文字了

Push successful! App '****' available at http://****.cfapps.io[/code] 
9. 访问一切正常。

10. 另:有的同学,可能还是报数据库连接错误,有可能是session存储的配置方式不对,应该如下:

app.use(express.session({
secret: settings.cookieSecret,
key: settings.db,
cookie: {maxAge: 1000 * 60 * 60 * 24 * 30},//30 days
store: new MongoStore({
url: mongourl
})
}));
配置URL,不要配置db,具体参见http://kcbanner.github.io/connect-mongo/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息