每当我使用node.js向服务器发出http请求时,http请求函数内的console.logs都会输出两次。我使用以下代码设置了一个服务器,并在 firefox 上使用 localhost:8888 发出请求(localhost:8888):
var http = require('http');
var url = require('url');
function onRequest(request, response) {
var pathname = url.parse(request.url, true).pathname;
console.log("Your url pathname is " + pathname);
response.write("Did you get your response?");
}
var new_server = http.createServer(onRequest).listen(8888);
控制台打印:
Your url pathname is /
Your url pathname is /favicon.ico
我的问题是:
- 为什么请求发送了两次?
- 尽管我在请求网址中的套接字号后面没有指定任何内容,但为什么第二个请求上的路径名是 favicon.ico?
- 有什么办法可以解决这两个问题吗?
谢谢。
最佳答案
显然,请求没有发送两次,而是两个请求。
这是浏览器请求图标。这就是浏览器所做的事情。
关于javascript - Nodejs http请求console.log函数打印两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25410139/