一次請求為什么createserver的毀掉函數中打印兩次?
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello China\n');
console.log('向客戶端發送信息');
}).listen(8888);
上面的代碼:當訪問請求時,會打印兩次“向客戶端發送信息”
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello China\n');
console.log('向客戶端發送信息');
}).listen(8888);
上面的代碼:當訪問請求時,會打印兩次“向客戶端發送信息”
2017-05-16
舉報
2017-05-23
謝謝了,在http://cnodejs.org/topic/518772806d38277306804020找到解釋了,并且也驗證通過了;
var http=require('http');var i=0;var req=function(req,res){
i=i+1;console.log(i,req.url);
res.writeHead(200,{'Content-Type':'text/plain'});
res.end('Hello World\n');};http.createServer(req).listen(8888,"127.0.0.1");console.log('Server running...');
這時輸出為:?
Server running…?
1:/?
2:/favicon.ico
我們發現favicon也被當做是一次請求,故被執行了兩次,另外有意思的地方就是把res.end(‘Hello World\n’);注釋或刪除,console.log(i)就不會被執行兩次了。
2017-05-21
request時,執行一次,服務返回response又被執行一次。