为什么node.js,http-server,显示有两个请求来了



我有以下使用node.js的简单http服务器设置:

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 Filesnodejsnode.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"...,也会请求它,因为规范定义了默认文件路径。

查找有关请求的最佳方法是:

  • 客户端,通过打开开发人员工具并查看"网络"选项卡。
  • 服务器端,通过日志记录req.url

相关内容

最新更新