官术网_书友最值得收藏!

Handling favicon requests

When visiting a URL via a browser, you will often notice a little icon in the browser tab or in the browser's address bar. This icon is an image named favicon.ico, and it is fetched on each request. As such, an HTTP GET request normally combines two requests—one for the favicon, and another for the requested resource.

Node developers are often surprised by this doubled request. Any implementation of an HTTP server must deal with favicon requests. To do so, the server must check the request type and handle it accordingly. The following example demonstrates one method of doing so:

const http = require('http');
http.createServer((request, response) => {
if(request.url === '/favicon.ico') {
response.writeHead(200, {
'Content-Type': 'image/x-icon'
});
return response.end();
}
response.writeHead(200, {
'Content-Type': 'text/plain'
});
response.write('Some requested resource');
response.end();

}).listen(8080);

This code will simply send an empty image stream for the favicon. If there is a favicon to send, you would simply push that data through the response stream, as we've discussed previously.

主站蜘蛛池模板: 宜章县| 得荣县| 马公市| 浦东新区| 北宁市| 连城县| 青冈县| 建湖县| 和顺县| 绥阳县| 肇东市| 乌鲁木齐县| 兴隆县| 钦州市| 富川| 大埔县| 洛南县| 青岛市| 沙坪坝区| 土默特左旗| 祁东县| 丹巴县| 酉阳| 曲阳县| 衡山县| 澄城县| 奈曼旗| 双辽市| 龙州县| 漳平市| 宜都市| 项城市| 苍山县| 垦利县| 惠来县| 固阳县| 台湾省| 新竹县| 陕西省| 普格县| 资溪县|