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

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.

主站蜘蛛池模板: 阜平县| 潼南县| 通许县| 读书| 商水县| 绩溪县| 喜德县| 高平市| 陇川县| 东至县| 华亭县| 清水河县| 沙洋县| 嘉黎县| 长海县| 遂溪县| 双柏县| 临沧市| 南投县| 望都县| 乌鲁木齐市| 郓城县| 醴陵市| 枣庄市| 武鸣县| 南岸区| 武穴市| 遂川县| 兖州市| 岳西县| 北票市| 裕民县| 酉阳| 敦煌市| 宾阳县| 卢氏县| 玉环县| 开远市| 衡水市| 鹤壁市| 斗六市|