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
这是为什么?
可能是用于图标的请求。查看浏览器开发人员工具中的网络选项卡。 –
@dystroy是的!你是对的,我试着记录'req.url'并猜测是什么?它显示了对favicon.ico的请求。我想我快要问:) – Krimson
尝试记录更多的请求对象...它会告诉你两个请求之间的区别。 – Stuart