没有要求的favicon

我正在写一个简单的node.js文件服务器使用节点的http模块(我没有使用EXPRESS)。

我注意到我的初始GET请求正在触发和所有后续的GET请求正在为CSS和JavaScript; 但是,我没有收到对favicon的请求。 即使我在页面检查员看,我没有任何错误,favicon没有显示在资源中。

HTML

// Inside the head of index.html <link rel="shortcut icon" href="img/favicon.ico" type="image/x-icon"> <link rel="icon" href="img/favicon.ico" type="image/x-icon"> 

Node.js的

 http.createServer(function(req, res){ // log each req method console.log(`${req.method} request for ${req.url}`); }).listen(3000) 

有一个默认图标是胡须,但不是我的自定义图标。 我在这里错过了什么?

以防万一与问题有关。 我正在使用节点v4.2.4

编辑

我认为这与我如何阅读和提供文件有关。

 if ( req.url.match(/.ico$/) ){ var icoPath = path.join(__dirname, 'public', req.url); var fileStream = fs.createReadStream(icoPath, "BINARY"); res.writeHead(200, {"Content-Type": "image/x-icon"}); fileStream.pipe(res) 

我应该不使用readstream吗? 是编码二进制或utf-8或别的什么?

我玩了你的回购代码,并做了一些改变,看看我能否服务我的favicon或不。 我发现了一些奇怪的东西:

  • favicon服务是棘手和神秘的(我的观点)
  • 铬曾经有一个错误,或可能仍然有关于图标(谷歌请,有很多结果)
  • 看起来像浏览器cachingfavicon ,强制刷新和不同的方法来使浏览器获得新的/更新的favicon ,看到这里
  • 我发现一旦Chrome浏览器在随后的请求中被提供favicon,那么就没有更多的favicon请求。 直到你改变html文件中的faviconhref ,并迫使浏览器提出新的要求。

我复制了一些你的代码来重现这个问题,并让它工作。 我决定服务的favicon不同的看法如下:

server.js

 if(req.url.match("/requestFavicon") ){ var img = fs.readFileSync('public/favicon.ico'); res.writeHead(200, {'Content-Type': 'image/x-icon' }); res.end(img, 'binary'); } 

的index.html

 <link rel="shortcut icon" type="image/x-icon" href="/requestFavicon"/> 

做了一个nodemon server.js并用Chrome浏览器制作http://192.168.1.18:8080请求。 terminal显示如下:

 GET request for / GET request for /requestFavicon 

在浏览器中显示的favicon.ico (微型车辆从这里采取)看到如下:

在这里输入图像描述

在显示上面的favicon之后,任何后续的http://192.1668.18:8080不会产生favicon请求,但是只在nodejs的terminal显示如下请求

 GET request for / 

同样在浏览器开发工具networking选项卡中没有favicon相关的请求。

在这一点上,我假设浏览器已经caching了它,并且根本没有请求,因为它已经有了。 所以我GOOGLE了,并遇到这个问题,以强制刷新favicon请求。 所以,让我们改变index.html (和server.js )中的favicon href然后重试

 <link rel="shortcut icon" type="image/x-icon" href="/updatedRequestFavicon"/> 

现在重试访问http://192.168.1.18:8080并观察nodejsterminal以及铬开发人员控制台,我得到以下内容:

 GET request for / GET request for /UpdatedRequestFavicon 

在这里输入图像描述

现在我想完全改变favicon并把一个新的。 我添加了这个更新了server.js并刷新了浏览器。 令人惊讶的没有控制台loginnodejs(新的favicon ),浏览器开发工具newtork控制台没有请求(所以提供caching值)。

为了强制浏览器获取新的favicon ,我想更改index.htmlfaviconhref ,并用new href更新server.js ,然后看brwoser是否被强制请求更新的favicon或继续使用caching的。

 <link rel="shortcut icon" type="image/x-icon" href="/NewFavicon"/> if(req.url.match("/NewFavicon") ){ var img = fs.readFileSync('public/favicon_new.ico'); res.writeHead(200, {'Content-Type': 'image/x-icon' }); res.end(img, 'binary'); } 

改变。 更改由nodemon重新加载,刷新浏览器,结果如下:

 GET request for / GET request for /NewFavicon 

在这里输入图像描述

你的问题可能与之有关

  1. 浏览器已经有一个caching的图标,因此,不要求它
  2. 您在server.js中没有正确地提供favicon
  3. 别的东西

如果你想testing我的代码,感觉自由,这里是server.js

 var http = require('http'); var fs = require('fs'); var path = require('path'); var server = http.createServer(function(req, res) { // Log req Method console.log(`${req.method} request for ${req.url}`); //res.writeHead(200); //res.end('index.html'); // Serve html, js, css and img if ( req.url === "/" ){ fs.readFile("index.html", "UTF-8", function(err, html){ res.writeHead(200, {"Content-Type": "text/html"}); res.end(html); }); } if(req.url.match("/NewFavicon") ){ console.log('Request for favicon.ico'); var img = fs.readFileSync('public/favicon_new.ico'); res.writeHead(200, {'Content-Type': 'image/x-icon' }); res.end(img, 'binary'); //var icoPath = path.join(__dirname, 'public', req.url); //var fileStream = fs.createReadStream(icoPath, "base64"); //res.writeHead(200, {"Content-Type": "image/x-icon"}); //fileStream.pipe(res); } }); server.listen(8080); 

这里是index.html

 <!DOCTYPE html> <html> <head> <title>nodeCAST</title> <link rel="shortcut icon" type="image/x-icon" href="/NewFavicon"/> <!--<link rel="shortcut icon" href="img/favicon.ico" type="image/x-icon"> <link rel="icon" href="img/favicon.ico" type="image/x-icon">--> </head> <body> <p>I am the index.html</p> </body> </html> 

我把favicon.ico放在/ public目录下。 祝你好运。

编辑

testing了Chrome浏览器版本47.0.2526.111米

节点v4.2.4

当浏览器加载页面时 ,浏览器会自动向favicon.ico发送请求 这个代码, endres ,将收到favicon请求:

 http.createServer(function(req, res) { console.log(`${req.method} ${req.url}`); res.writeHead(200); res.end(); }).listen(3000); 

正如你在运行日志中看到的那样:

 GET / GET /favicon.ico