What is the use of serve favicon from Node.js server ?
When the browser loads a website for the first time, it will automatically request /favicon.ico (GET) to load favicon. A favicon is a small size file as known as website icon, tab icon, URL icon or bookmark icon. The serve-favicon module is used to serve favicon from the NodeJS server.
Why to use this module?
- User agents request frequently and indiscriminately for favicon. Use this middleware before your logger middleware to exclude these request from logs.
- favicon.ico is small size file, this module caches the favicon in memory to give better performance by skipping dick access.
- This module provides an ETag for the icon.
- This module provides compatible Content-Type for the icon.
Project Setup and Module Installation:
Step 1: Create a NodeJS application and name it Project using the following command.
mkdir Project && cd Project npm init -y
Step 2: Install the dependency modules using the following command.
npm install express serve-favicon
Project Directory: It will look like this.
Step to run the application: Run the server.js using the following command
Output: Open the browser and go to http://localhost:8080/, we will see the following output on screen.
Note: Remember, serve-favicon is only serving default, implicit favicon, which is GET /favicon.ico. Use serve-static for vendor-specific icons that require HTML markup.