- 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.
- EDA技術(shù)與VHDL編程
- 工業(yè)控制網(wǎng)絡(luò)安全技術(shù)與實(shí)踐
- 通信簡史:從信鴿到6G+
- 計(jì)算機(jī)網(wǎng)絡(luò)工程實(shí)用教程(第2版)
- Yii Application Development Cookbook(Second Edition)
- 雷達(dá)饋線技術(shù)
- 物聯(lián)網(wǎng)技術(shù)與應(yīng)用
- 網(wǎng)絡(luò)安全應(yīng)急響應(yīng)技術(shù)實(shí)戰(zhàn)
- jQuery Mobile Web Development Essentials
- 語音信號處理及Blackfin DSP實(shí)現(xiàn)
- Microsoft Power Platform Enterprise Architecture
- 沖擊:5G如何改變世界
- Laravel Application Development Cookbook
- 物聯(lián)網(wǎng),So Easy!
- 從物聯(lián)到萬聯(lián):Node.js與樹莓派萬維物聯(lián)網(wǎng)構(gòu)建實(shí)戰(zhàn)