Favicon.ico 未被请求
Favicon.ico not being requested
有没有一种方法可以只使用 'http' 和 'fs' 模块为 node.js 设置一个 HTML 页面的图标(没有 express)?
我的目录中有 index.html、main_page.css 和 favicon.ico 文件。
客户端连接时,收到index.html:
<!DOCTYPE html>
<html>
<head>
<title>Site</title>
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<link rel="stylesheet" type="text/css" href="css/main_page.css" />
</head>
<body>
</body>
</html>
然后服务器收到对.css文件的请求并将其发送给客户端。
该页面的样式应有,因此没有理由显示代码。
问题是,为什么客户端自己要.css文件而不要.ico?
这是服务器上为文件提供服务的代码:
var http = require('http');
var fs = require('fs');
http.createServer(function(req, res) {
switch (req.url) {
case '/': {
get_file('index.html', res);
break;
}
case '/css/main_page.css': {
get_file('css/main_page.css',res);
break;
}
case '/favicon.ico': {
get_file('favicon.ico', res);
break;
}
default: {
res.statusCode = 404;
res.end("Not found");
}
}
}).listen(8081);
function get_file(path, res) {
//dont mind my ROOT ;0
fs.readFile(ROOT + path, function(err, content) {
if(err) {
console.log(err.message);
} else {
console.log(path);
res.end(content);
}
})
}
favicon.ico
文件被Chrome缓存,所以一旦被请求过一次,在缓存过期之前不会再被请求。要为 favicon.ico
生成新请求,您需要刷新 Chrome 中的页面。
有没有一种方法可以只使用 'http' 和 'fs' 模块为 node.js 设置一个 HTML 页面的图标(没有 express)?
我的目录中有 index.html、main_page.css 和 favicon.ico 文件。
客户端连接时,收到index.html:
<!DOCTYPE html>
<html>
<head>
<title>Site</title>
<link rel="icon" href="favicon.ico" type="image/x-icon" />
<link rel="stylesheet" type="text/css" href="css/main_page.css" />
</head>
<body>
</body>
</html>
然后服务器收到对.css文件的请求并将其发送给客户端。
该页面的样式应有,因此没有理由显示代码。
问题是,为什么客户端自己要.css文件而不要.ico?
这是服务器上为文件提供服务的代码:
var http = require('http');
var fs = require('fs');
http.createServer(function(req, res) {
switch (req.url) {
case '/': {
get_file('index.html', res);
break;
}
case '/css/main_page.css': {
get_file('css/main_page.css',res);
break;
}
case '/favicon.ico': {
get_file('favicon.ico', res);
break;
}
default: {
res.statusCode = 404;
res.end("Not found");
}
}
}).listen(8081);
function get_file(path, res) {
//dont mind my ROOT ;0
fs.readFile(ROOT + path, function(err, content) {
if(err) {
console.log(err.message);
} else {
console.log(path);
res.end(content);
}
})
}
favicon.ico
文件被Chrome缓存,所以一旦被请求过一次,在缓存过期之前不会再被请求。要为 favicon.ico
生成新请求,您需要刷新 Chrome 中的页面。