为什么node.js,http-server,显示有两个请求来了? [英] Why does node.js, http-server, show that there are two request coming?
本文介绍了为什么node.js,http-server,显示有两个请求来了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 node.js 进行了以下简单的 http 服务器设置:
I have the following simple http-server setup using node.js:
var http = require('http');
var port = 12311
http.createServer(function (req, res) {
console.log("Incomming request from " + req.connection.remoteAddress);
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end("test string");
}).listen(port);
console.log("Listening on " + port);
如您所见,当请求进来时,我将其记录到控制台.现在,当我浏览到 localhost:12311
时,控制台显示有两个连接:
As you can see, when a request comes in, I log it to the console. Now when I browse to localhost:12311
the console shows that two connections have come in:
"E:\Program Files\nodejs\node.exe" hello-world-server.js
Listening on 12311
Incomming request from 127.0.0.1
Incomming request from 127.0.0.1
这是为什么?
推荐答案
通常是对 favicon.ico
的请求.即使你没有,如果你没有在标题中设置相关的 <link rel="shortcut icon"...
,它也会被请求,因为规范定义了一个默认的文件路径.
It's usually the request for the favicon.ico
. Even if you don't have one, it's requested as the norm defines a default file path if you don't set the relevant <link rel="shortcut icon"...
in the header.
查找请求的最佳方法是:
The best ways to find about the requests are :
- 客户端,打开开发者工具并查看网络标签.
- 服务器端,通过记录
req.url
- client side, by opening the developer tools and looking at the network tab.
- server side, by logging
req.url
这篇关于为什么node.js,http-server,显示有两个请求来了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文