Node.js是double console.log输出

JDi*_*522 13 node.js console.log

我正在学习Node.js,我想了解"为什么"代码吐出重复的console.log输出但只有一个response.write输出.

继承我的简单代码示例:

var http = require('http');

http.createServer(function(request, response){
    response.writeHead(200, {'Content-type': 'text/plain'});
    console.log('hello 1');
    response.write('Hello world');
    console.log('hello 2');
    response.end();
}).listen(8000);
Run Code Online (Sandbox Code Playgroud)

在我的控制台/终端上,我得到:

你好1

你好2

你好1

你好2

谢谢.

c.P*_*.u1 23

某些浏览器还会发送请求以找到favicon.ico文件.由于默认情况下该文件不存在,因此浏览器(特别是Chrome)将始终发送两个请求:一个用于最初请求的文件,另一个用于favicon.ico.这是Chrome中的已知错误,已在第29版中修复.然而,Firefox仅针对第一个请求请求favicon.ico.如果您console.log是请求URI 路径,则必须查看请求localhost:8000/favicon.ico.

var http = require('http');

http.createServer(function(request, response){
    response.writeHead(200, {'Content-type': 'text/plain'});
    if(request.url === '/favicon.ico') {
        console.log('Favicon was requested');
    }
    console.log('hello 1');
    response.write('Hello world');
    console.log('hello 2');
    response.end();
}).listen(8000);
Run Code Online (Sandbox Code Playgroud)