我认为首先要注意的是,CloudFoundry 将很快支持 Node.JS 应用程序的自动重新配置,就像我们目前对 Rails 所做的一样徒劳无功。这意味着能够在本地开发使用 MySQL 的 Node.JS 应用程序,并且在将其部署到 CloudFoundry 时,它将自动重新配置为使用绑定服务。
在该功能上线之前,正如我所说的不会很长时间,您将不得不使用 VCAP 环境变量在运行时配置连接。这里有一个很好的例子来说明这种方法与 Node.JS 一起使用 - http://www.mihswat.com/2011/05/04/getting-started-with-cloud-foundry-using-a-node-js -and-mongodb-application/尽管对于 MongoDB,方法是完全一样的。
需要特别注意的部分是作者使用以下代码将环境读取到哈希中;
var boundServices = process.env.VCAP_SERVICES
? JSON.parse(process.env.VCAP_SERVICES) : null;
然后使用它们来初始化连接;
if (boundServices == null) {
db = mongoose.connect('mongodb://localhost/analytics');
} else {
credentials = boundServices['mongodb-1.8'][0]['credentials'];
db = mongoose.createConnection("mongo://"
+ credentials["username"]
+ ":" + credentials["password"]
+ "@" + credentials["hostname"]
+ ":" + credentials["port"]
+ "/" + credentials["db"]);
}
如果您需要任何进一步的指示,请告诉我。