- Mastering Node.js(Second Edition)
- Sandro Pasquali Kevin Faaborg
- 168字
- 2021-07-02 19:28:51
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.
- Application Development with Qt Creator(Second Edition)
- Cisco OSPF命令與配置手冊
- 微商之道
- 物聯(lián)網(wǎng)工程規(guī)劃技術(shù)
- 物聯(lián)網(wǎng)(IoT)基礎(chǔ):網(wǎng)絡(luò)技術(shù)+協(xié)議+用例
- 工業(yè)控制網(wǎng)絡(luò)安全技術(shù)與實踐
- Hands-On Industrial Internet of Things
- HCNA網(wǎng)絡(luò)技術(shù)
- 物聯(lián)網(wǎng)長距離無線通信技術(shù)應(yīng)用與開發(fā)
- Mastering Dart
- Microservice Patterns and Best Practices
- 基于性能的保障理論與方法
- Scala Design Patterns.
- React Cookbook
- LwIP應(yīng)用開發(fā)實戰(zhàn)指南:基于STM32