When it comes to writing routes directly, it can quickly become overwhelming and difficult to manage, especially as the number of routes grows. This is where the MVC pattern comes into play, dividing the application into modules or logical blocks based on their specific functionalities. For instance, in a hypothetical hospital management system, you might have modules for authentication, billing, payroll, medical stock, patients, and more. In an MVC setup, it's common practice to have a separate controller for each module. Express offers middleware functionality, also known as Routers, to connect these controllers to their respective API routes (think of it as a roadmap linking routes to controllers).
Once you've defined routes for each module using middleware, you can utilize these routes within your application. These routes handle incoming requests and pass parameters to the appropriate controller for processing.
Learn more about Middleware and Routers here: https://www.tutorialspoint.com/expressjs/expressjs_routing.htm
From a code quality perspective, breaking down the code into modular components and using routers to establish connections between them makes it easier for others to understand. It also offers a clearer overview of the application structure, making it simpler to add new modules or functionalities.
For further insights on building a production-ready express app, check out:
https://www.freecodecamp.org/news/how-to-write-a-production-ready-node-and-express-app-f214f0b17d8c/