concept middleware function in category node

This is an excerpt from Manning's book Get Programming with Node.js.
One last note on Express.js routes: I talked about how Express.js is a type of middleware because it adds a layer between a request being received and that request being processed. This feature is great, but you may want to add your own custom middleware. You may want to log the path of every request made to your application for your own records, for example. You can accomplish this task by adding a log message to every route or by creating the middleware function in listing 9.4. This listing defines a middleware function with an additional next argument, logs the request’s path to your terminal console, and then calls the next function to continue the chain in the request-response cycle.
next is provided as a way of calling the next function in your request-response execution flow. From the time a request enters the server, it accesses a series of middleware functions. Depending on where you add your own custom middleware function, you can use next to let Express.js know that your function is complete and that you want to continue to whatever function is next in the chain.
You can also specify a path for which you’d like your middleware function to run. app.use("/items", <callback>), for example, will run your custom callback function for every request made to a path starting with items. Figure 9.1 shows how middleware functions can interact with a request on the server.
In this lesson, you learned how to build routes and middleware functions with Express.js. Then you used middleware functions to work with Express.js in analyzing request body contents. At the end of the lesson, you learned about MVC and saw how routes can be rewritten to use controllers in your application. In lesson 10, you jump into views and a rich feature known as layouts. With these tools, you can build your views faster.