NodeJS服务器为每个请求增加两个variables
当这个代码运行时, i
每次增加两个,我不能在文档中指出,否则为什么会这样。 我希望每个请求的增量为1,但事实并非如此。 为什么这是这样的行为?
var http = require('http'); var i = 0; http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Number: ' + i + '\n'); i++; }).listen(8000, '127.0.0.1');
console.log(req.url);
你会注意到这个url是/
和/favicon.ico
浏览器喜欢为您提供favicon.ico的请求。 这就是为什么你得到2个请求。