What is middleware in Express.js?

Beginner

Answer

Middleware functions are functions that have access to the request object (req), response object (res), and the next middleware function in the application's request-response cycle. Middleware can execute code, modify req/res objects, end the request-response cycle, or call the next middleware.

// Custom middleware
app.use((req, res, next) => {
  console.log('Time:', Date.now());
  next(); // Call next to continue to the next middleware
});