Incr似乎运行两次 – 为什么?

我很难得到节点,redis和async做我想要的。 我正在尝试非常基本的事情来掌握redirect控制stream的模式。 在这里,如果比较key0 > key1为真,我保存一个计数器variables“success”。 它们现在是静态的,所以它总是如此。 我唯一希望改变的是增加success 。 我刷新浏览器重新运行比较,并再次增加success

我的麻烦是:刷新页面时, success跳转2.我试着用incr进行callback,但看起来只有get -type命令有callback。 我有一个client.end(); 在我的脚本,但它阻止我重新加载页面,所以我评论它。 我怀疑这是我的问题的来源。 如果是这样, client.end属于哪里?

 var http = require("http"); var redis = require("redis"); var async = require("async"); client = redis.createClient(); http.createServer(function(request, response) { // key "success" set to 0 externally, through redis-cli; client.set("key0", "19"); client.set("key1", "11"); response.writeHead(200, {"Content-Type": "text/plain"}); async.series([ shortcut("key0"), shortcut("key1"), shortcut("success") ], function(err, results){ if (results[0] > results[1]) { client.incr("success", function(err, reply) { response.write("incr done"); }); response.write(results[0] + "\n\n"); response.write(results[1] + "\n\n"); response.write(results[2]); } response.end(); // client.quit(); }); }).listen(8000); function shortcut(key) { return function(callback) { client.get(key, function(err, reply) { callback(null, reply); } ); } } 

您的浏览器很可能会请求favicon.ico ,从而生成额外的请求,它会再次运行您的代码。