2014-03-06 41 views
1

我如何使用Node.js以下简单的HTTP服务器设置:为什么node.js,http-server显示有两个请求会来?

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控制台显示两个连接都来了:

"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 

这是为什么?

+1

可能是用于图标的请求。查看浏览器开发人员工具中的网络选项卡。 –

+0

@dystroy是的!你是对的,我试着记录'req.url'并猜测是什么?它显示了对favicon.ico的请求。我想我快要问:) – Krimson

+0

尝试记录更多的请求对象...它会告诉你两个请求之间的区别。 – Stuart

回答

2

这通常是favicon.ico的要求。即使你没有一个,如果你没有在标题中设置相关的<link rel="shortcut icon"...,它也会被要求作为标准定义一个默认的文件路径。

找到的有关要求最好的方法是:

  • 客户端,通过打开developer tools来看,在网络标签。
  • 服务器侧,通过登录req.url
相关问题