0
我今天安装了node.js。我从例子“Hello World”服务器开始,并开始对它进行黑客攻击。没过多久,我有这样的:为什么CreateServer函数处理程序被调用两次?
var http = require('http');
var count = 0;
http.createServer(function handler(req, res) {
console.log('yeah ' + count++);
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<b>Zoot!</b><br><em>yeah</em>\n');
}).listen(1338, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1338/');
当我浏览到该端口,我的控制台日志显示在CreateServer处理程序被调用两次 - 我增加了计数,所以我可以肯定。 2'浏览'后:
Server running at http://127.0.0.1:1338/
yeah 0
yeah 1
yeah 2
yeah 3
为什么会发生这种情况?
编辑
蒂姆库珀是正确的。
我更改的日志记录行
console.log('yeah ' + req.url +' '+ count++);
,得到了这样的结果
Server running at http://127.0.0.1:1338/
yeah/0
yeah /favicon.ico 1
yeah/2
yeah /favicon.ico 3
您的浏览器很可能从服务器以及HTML页面请求'/ favicon.ico'。 –
好吧,我会开始寻找。谢谢! 更多来。 –
好的,这很容易。谢谢@TimCooper。将您的评论发布为答案。 –