Middleware in Express

 Middleware in Express: Middleware in Express is a function that is executed before a route handler. It can modify the request or response objects, or execute additional logic before passing control to the next middleware or route handler. Here's an example of how to define a middleware in Express:

javascript


const express = require('express');

const app = express();


app.use((req, res, next) => {

  console.log('Request received at:', new Date());

  next();

});


app.get('/', (req, res) => {

  res.send('Hello World!');

});


app.listen(3000, () => {

  console.log('Server listening on port 3000');

});

No comments:

Post a Comment

The Importance of Cybersecurity in the Digital Age

 The Importance of Cybersecurity in the Digital Age Introduction: In today's digital age, where technology is deeply intertwined with ev...