expressjsHow do I set up a favicon with Express.js?
To set up a favicon with Express.js, you must first create a favicon.ico file and save it in the root directory of your project. Then, you must require the serve-favicon middleware and include it in your Express.js app. Here is an example of how to do this:
const express = require('express');
const favicon = require('serve-favicon');
const path = require('path');
const app = express();
app.use(favicon(path.join(__dirname, 'favicon.ico')));
This will serve the favicon.ico file whenever the /favicon.ico path is requested. No output is produced.
The code above consists of the following parts:
const express = require('express');- This line imports the Express.js library.const favicon = require('serve-favicon');- This line imports theserve-faviconmiddleware.const path = require('path');- This line imports thepathlibrary, which is used for manipulating file paths.const app = express();- This line creates an Express.js app.app.use(favicon(path.join(__dirname, 'favicon.ico')));- This line uses theserve-faviconmiddleware with the path to thefavicon.icofile.
Helpful links
More of Expressjs
- How can I disable the X-Powered-By header in Express.js?
- How do I use adm-zip with Express.js?
- How do I use Express.js to parse YAML files?
- How can I use express-zip js to zip and download files?
- How can I use Express.js and Vite together for software development?
- How can I create and use models in Express.js?
- How do I download a zip file using Express.js?
- How can I use Express.js to generate a zip response?
- How do I set the time zone in Express.js?
- How can I use Express.js with TypeScript?
See more codes...