2017-05-07 113 views
0

當我將此作爲服務器運行時,goto localhost:8080'已保存!'在控制檯中打印兩次。並且'Honey Bee'在mynewfile1.txt中追加了兩次爲什麼?在nodejs中調用兩次函數

var http = require('http'); 
var fs = require('fs'); 

http.createServer(function (req, res) { 
    res.writeHead(200, { 'Content-Type': 'text/html' }); 
    res.write('Hello World!'); 

    fs.readFile('head.html', function (err, data) { 
     res.write(data); 
     res.end(); 
    }); 

    fs.appendFile('mynewfile1.txt', 'Honey Bee', function (err) { 
     if (err) throw err; 
     console.log('Saved!'); 
    }); 
}).listen(8080); 

回答

0

這是因爲你的瀏覽器makes an additional request獲取favicon.ico。使用curl發出一個HTTP請求:

curl http://localhost:8080