我使用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 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"...
,也会要求它,因为规范定义了默认文件路径。在标题中。
查找请求的最佳方法是:
- 客户端,通过打开 developer tools并查看网络选项卡。
- 服务器端,通过日志记录
req.url
关于node.js - 为什么node.js,http-server,显示有两个请求到来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22219463/