求助,get请求为什么发出了两次
发布于 12 年前 作者 jtyjty99999 6749 次浏览 最后一次编辑是 8 年前
var http = require('http'),
q = require('querystring');

http.createServer(function (req, res) {

		var options = {
			host : '192.168.1.2',
			port : 7379,
			path : '/INCR/soscount',
			method : 'get'
		};

	//服务端发出请求get
	http.get(options,function(response){
		res.writeHead(200, {'Content-Type': 'text/plain'});
		var pageData = "";
		response.setEncoding('utf8');
		//接受数据,保存
		response.on('data', function (chunk) {
		  pageData += chunk;
		});

		//数据接收完毕, 输出到客户端
		response.on('end', function(){
		  res.write(pageData);
		  res.end();
		});

	});

}).listen(4000);
console.log("httpServer is listening at port 4000")

想用nodejs发起一个webredis的测试, 直接浏览器访问 http://192.168.1.2:7379/INCR/soscount 的时候 每次访问 数字会+1 但是如果通过nodejs 访问,每次访问数字会+2 这是为什么呢。。。

8 回复

因为多访问了一次"/favicon.ico"

明白了,要路由一下,感谢

@clamingo 跪求麻烦给我补充一点知识 1.为什么会多一次/favicon.ico 2.这个请求是所有浏览器都默认请求的还是个别现象 3.这样的2次请求能通过设置修改为一次吗?

@a272121742 1、请求/favicon.ico是用于显示网站的图标,如右图:enter image description here 2、大多数浏览器都会发出这样的请求,算是“默认”的吧 3、只要你返回一个有效的的/favicon.ico文件,并且指定expire时间,相信浏览器不会每次都重新发出请求的。

@a272121742 connect中是专门有一个中间件来处理这个问题的,参考这里:http://www.senchalabs.org/connect/favicon.html

@jtyjty99999 如上所言。周末断网状态,不好意思。

回到顶部