manage favicon with express app
- Install the 'serve-favicon' package using npm:
npm install serve-favicon
- Import the 'express' and 'serve-favicon' modules in your Express application:
const express = require('express');
const favicon = require('serve-favicon');
- Specify the path to your favicon.ico file using the 'path' module:
const path = require('path');
const faviconPath = path.join(__dirname, 'public', 'favicon.ico');
- Create an instance of the Express application:
const app = express();
- Use the 'favicon' middleware and specify the path to the favicon.ico file:
app.use(favicon(faviconPath));
- Define a route for your application:
app.get('/', (req, res) => {
res.send('Hello, World!');
});
- Set up the Express application to listen on a specific port (e.g., 3000):
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
Create a 'public' folder in your project directory and place the 'favicon.ico' file inside it.
Run your Express application:
node your-app.js
Replace 'your-app.js' with the actual filename of your Express application file. Access your application in a web browser, and the favicon.ico file should be served automatically.