Search code examples
node.jsexpressmodel-view-controller

How and why does Express uses MVC pattern?


I've tried to the net to understand why Express uses MVC to structure the code, but I haven't really got any explanation. Can anyone please describe how Express can be used to structure codes that follows the MVC design pattern?


Solution

  • It's actually pretty simple.

    You just modularize your code in different modules and folders and import/export with commonJS (require as import/export isn't yet that popular in Node).

    You can create a model folder where you can place all of your schema declarations, a controller folder where you'd place all of your logics to handle the routes, a route folder where you'd place all of your routes (thanks to express router) and a views folder where you'd place all of your pug, ejs etc. files (if you'd like to make a server-side rendered app).

    All the client-side code and static files usually go into a public folder and voilà, here is your MVC structure with Express.