Application-level Middleware: Application-level middleware is bound to the entire Express application. It is defined using app.use()
or related methods and is executed for every incoming request. It’s suitable for tasks like logging, setting headers, and general pre-processing.
Example: Logging middleware
const express = require('express');
const app = express();
// Application-level middleware
app.use((req, res, next) => {
console.log(Received ${req.method} request at ${req.url});
next();
});
app.get(‘/’, (req, res) => {
res.send(‘Hello, Express!’);
});
app.listen(3000, () => {
console.log(‘Server is running on port 3000’);
});