每个请求的NodeJS服务器增量变量增加2 [英] NodeJS server incrementing variable by two for every request

查看:59
本文介绍了每个请求的NodeJS服务器增量变量增加2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行此代码时, i 每次都会增加2,因此我无法查明文档中的内容或其他原因.我希望每个请求的增量为1,但事实并非如此.为什么这样表现呢?

When this code is run, i is incremented by two every time and I can't pinpoint in the documentation or otherwise why this would be the case. I'd expect the increment to be by one for each request, but it's not. Why is this behaving the way it is?

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);

您会注意到网址是//favicon.ico

浏览器喜欢为您请求favicon.ico.这就是为什么您收到2个请求的原因.

Browsers like making requests to favicon.ico for you. That's why you get 2 requests.

这篇关于每个请求的NodeJS服务器增量变量增加2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆