2012-07-25 115 views
0

我正在製作一個使用Redis的應用程序。它可以在運行Ubuntu 12.04的計算機上正常工作,但在CloudFoundry上,它會給我一個錯誤。CloudFoundry與NodeJS的Redis故障

下面是應用程序的代碼:

var 
    http = require("http"), 
    redis = require("redis"), 
    cf = require("cloudfoundry"); 

cf.cloud; 

if (cf.redis["myredisservice"]) { 
    var rport = cf.redis["myredisservice"].credentials.port; 
    var rhost = cf.redis["myredisservice"].credentials.hostname; 
} else { 
    var rport = 6379; 
    var rhost = "127.0.0.1"; 
} 


http.createServer(function(req, res) { 
    var client = redis.createClient(rport, rhost); 

    res.writeHead(200, {"Content-Type": "text/html"}); 

    client.on("error", function(error) { 
     res.write("Error: " + error); 
    }); 

    res.write("Setting key 1<br>"); 
    client.set("key1", "My 1st String!", redis.print); 

    res.write("Getting key1<br>"); 
    client.get("key1", function(error, reply) { 
     res.write("Results for key1<br>"); 
     res.write(reply); 
     client.end(); 
     res.end(); 
    }); 
}).listen(cf.port || 3000); 

在CloudFoundry,當我運行這個程序,它給了我這樣的輸出:

Setting key 1

Getting key1

Error: Error: Ready check failed: ERR operation not permitted

我可以證實,其連接到Redis的服務使用CloudFoundry指定的端口和主機名。

所以我認爲它因爲其中一個命令而失敗。 :(

誰能告訴我,這是爲什麼錯誤發生

感謝:?。d

回答

3

您需要身份驗證的Redis以及試試這個:

var 
    http = require("http"), 
    redis = require("redis"), 
    cf = require("cloudfoundry"); 

cf.cloud; 

if (cf.redis["myredisservice"]) { 
    var rport = cf.redis["myredisservice"].credentials.port; 
    var rhost = cf.redis["myredisservice"].credentials.hostname; 
    var rpass = cf.redis["myredisservice"].credentials.password; 
} else { 
    var rport = 6379; 
    var rhost = "127.0.0.1"; 
} 


http.createServer(function(req, res) { 
    var client = redis.createClient(rport, rhost); 
    client.auth(rpass); 

    res.writeHead(200, {"Content-Type": "text/html"}); 

    client.on("error", function(error) { 
     res.write("Error: " + error); 
    }); 

    res.write("Setting key 1<br>"); 
    client.set("key1", "My 1st String!", redis.print); 

    res.write("Getting key1<br>"); 
    client.get("key1", function(error, reply) { 
     res.write("Results for key1<br>"); 
     res.write(reply); 
     client.end(); 
     res.end(); 
    }); 
}).listen(cf.port || 3000); 
+0

嗨格倫, :D – 2012-07-25 23:06:53

+1

如果您可以使用NodeJS在CloudFoundry上指定使用Redis,因爲您目前只有使用Ruby的指南:D – 2012-07-25 23:12:18

+1

很高興爲您提供幫助!下週http://blog.cloudfoundry.com(可能是t他繼續)瞭解關於node.js應用程序中服務自動配置的一些消息! – 2012-07-26 01:12:08