繁体   English   中英

为什么node.js(http服务器)显示有两个请求到来?

[英]Why does node.js, http-server, show that there are two request coming?

我使用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 ,控制台显示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

为什么是这样?

通常是对favicon.ico的请求。 即使您没有,也需要它,因为如果未在标题中设置相关的<link rel="shortcut icon"... ,则该规范将定义默认文件路径。

查找请求的最佳方法是:

  • 客户端,通过打开开发人员工具并查看“ 网络”标签。
  • 服务器端,通过记录req.url

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM