Html 没有法维康的请求
我正在使用node的http模块编写一个简单的node.js文件服务器(我没有使用EXPRESS) 我注意到我最初的GET请求正在启动,所有后续的GET请求都是针对css和javascript的;然而,我并没有收到关于favicon的请求。即使当我查看页面检查器时,我也没有任何错误,favicon也没有出现在参考资料中 HTMLHtml 没有法维康的请求,html,node.js,favicon,Html,Node.js,Favicon,我正在使用node的http模块编写一个简单的node.js文件服务器(我没有使用EXPRESS) 我注意到我最初的GET请求正在启动,所有后续的GET请求都是针对css和javascript的;然而,我并没有收到关于favicon的请求。即使当我查看页面检查器时,我也没有任何错误,favicon也没有出现在参考资料中 HTML // Inside the head of index.html <link rel="shortcut icon" href="img/favicon.ico"
// 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">
有一个默认的小胡子图标,但不是我的自定义图标。我错过了什么
以防万一这和问题有关。我正在使用节点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.ico本身发出请求。此代码将接收favicon请求,它将结束
res
ponse:
http.createServer(function(req, res) {
console.log(`${req.method} ${req.url}`);
res.writeHead(200);
res.end();
}).listen(3000);
正如您在运行日志中看到的:
GET /
GET /favicon.ico
我使用了你的回购代码,并做了一些更改,以查看我是否可以为favicon提供服务。我发现了一些奇怪的东西:
发球既棘手又神秘(我的观点)favicon
- Chrome曾经有一个bug,或者可能仍然有与favicon相关的bug(请谷歌搜索,有很多结果)
- 似乎浏览器缓存了favicon、强制刷新以及用于使浏览器获取新的/更新的favicon的不同方法,请参阅
- 我发现,一旦Chrome浏览器提供了favicon,那么在随后的请求中,就没有favicon请求了。除非您更改
文件中html
的favicon
,并强制浏览器发出新请求李>href
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"/>
<!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
(tiny vehicle.ico摘自),请参见以下内容:
显示上述favicon
后,任何后续http://192.1668.18:8080
未生成对favicon的请求,但仅在nodejs终端中显示以下请求
GET request for /
类似地,在浏览器开发人员工具网络选项卡中没有与favicon相关的请求
此时,我假设浏览器已经缓存了它,并且根本不请求,因为它已经有了它。所以我在谷歌上搜索并发现了这一点,以强制刷新favicon请求。因此,让我们更改index.html
(和server.js
)中的favicon
href,然后重试
<link rel="shortcut icon" type="image/x-icon" href="/updatedRequestFavicon"/>
现在,我想完全更改favicon的。我添加了一个更新了server.js
并刷新了浏览器。令人惊讶的是,nodejs中没有控制台日志(对于newfavicon
),browser-developer-tools-newwork控制台中没有请求(因此提供了缓存值)
要强制浏览器获取新的favicon
,我想更改index.html
中favicon
的href
,并使用新的href更新server.js
,然后查看brwoser是被迫请求更新的favicon还是继续使用缓存的favicon
<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');
}
你的问题可能与
浏览器已经有缓存的favicon,因此不请求它
您没有在server.js中正确地为favicon提供服务
别的
如果你想测试我的代码,请随意,这是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
<link rel="shortcut icon" type="image/x-icon" href="/requestFavicon"/>
<!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>
节点卡斯特
我是index.html
我将favicon.ico放在/public目录下。祝你好运
编辑
使用Chrome浏览器版本47.0.2526.111 m测试
节点v4.2.4这可能对您和Hmmm有所帮助,但没有帮助。就像我说的,它在本地有效。但当从节点提供页面服务时。没有请求。听起来像是缓存问题。清除浏览器缓存?@aaron,是的,没有胡子图标,但仍然没有对favicon的请求。就是这样,我加载页面时没有看到对favicon.ico的get请求。其他所有内容都会发出请求。您在日志中没有看到该请求吗?这没有道理,我这里的程序。。嗯…是的,你可以在我的网页上看到我的代码,这是很多信息。我觉得发生了一些奇怪的事情。我会尝试一下,然后很快给你回复。期待你的结果它成功了!因此,我使用了一个读取流,并将结果传输到响应。readFileSync和二进制编码似乎是我缺少的东西。谢谢你的帮助!我开始后悔我决定在这个问题上悬赏,但信心恢复了。干杯很高兴你的问题解决了,就赏金而言,希望你能在这个问题上多投几票。这很好,当我试图回答它时,我学到了很多。
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);
<!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>