变量在node.js http回调函数中增加两次 [英] Variable is incremented twice in node.js http callback function
本文介绍了变量在node.js http回调函数中增加两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
var http = require() http);
var i = 0;
function onRequest(request,response){
response.writeHead(200,{Content-Type:text / plain});
response.write(You're number+ i ++);
response.end();
}
http.createServer(onRequest).listen(8888);
我希望它的行为像一个页面浏览计数器,但每次刷新浏览器标签得到的结果似乎是 i = i + 2
而不是一个简单的增量。
解决方案
您的浏览器正在击中您的服务器 favicon.ico
。每个请求增加 i
,并请求 favicon.ico
计数。
使用 Fiddler 或 WireShark 自己看这个行为。
I was playing around with node.js and something strange happens when you run this code:
var http = require("http");
var i = 0;
function onRequest(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("You're number " + i++);
response.end();
}
http.createServer(onRequest).listen(8888);
I would expect it to behave like a page views counter, but with each refresh of the browser tab i get the result of what seems to be i=i+2
instead of a simple increment. Could someone explain this behavior to me?
解决方案
Your browser is hitting your server for favicon.ico
as well. Each request increments i
, and the request for favicon.ico
counts.
Use a tool such as Fiddler or WireShark to see this behavior yourself.
这篇关于变量在node.js http回调函数中增加两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文