What does adding a new function or class to the list of middleware means?

  express, javascript, middleware, node.js

I had a question that asked for creating a middleware that simply supports adding a new function or class to the list of middleware and executes it. I am not sure what it means. I wrote the following middleware so does this solution answers the question?

const express = require('express');
const app = express();
const configRoutes = require('./routes');

let count = 0;
let count_url = {};


app.use((req, res, next) => {
  count++;
  
  console.log(`Total number of requests: ${count}`);

  next();
})

app.use((req, res, next) => {
  console.log(`Request body: ${JSON.stringify(req.body)}`)
  console.log(`URL path: ${req.url}`)
  console.log(`HTTP method: ${req.method}`)

  next();
})

app.use((req, res, next) => {
  if (count_url[req.originalUrl]) {
    count_url[req.originalUrl] = count_url[req.originalUrl] + 1;
  }
  else {
    count_url[req.originalUrl] = 1;
  }

  console.log(`Total number of requests made to URL: ${req.originalUrl} is: ${count_url[req.originalUrl]}`);
  console.log(`------------------------------------------------------------------------------------------------------`);
  console.log(`------------------------------------------------------------------------------------------------------`);

  next();
})

app.use(express.json());
configRoutes(app);

Source: Ask Javascript Questions

LEAVE A COMMENT