Search code examples
node.jsexpressmodel-view-controllerarchitecture

Is it possible to have MVC in sub folders when using node.js express framework?


Is it possible to have MVC in sub folders (modules) when using node.js express framework?

Today I have this structure of my project:

  • Controllers
    • Blog/*
    • Poll/*
    • And many more
  • Models
    • Blog/*
    • Poll/*
    • And many more
  • View
    • Blog/*
    • Poll/*
    • And many more
  • Routes
    • Blog
    • Poll
    • And many more

Should it be possible to have this structure in a module way instead?:

  • Blog
    • Controllers/*
    • Models/*
    • View/*
    • routes.js
  • Poll
    • Controllers/*
    • Models/*
    • View/*
    • routes.js
  • And more modules

When I develop I feel I mostly work with one part of the project at the time. When I the current structure its a lot of moving between folders. However most of the time I only focus on "module". It will also be easier to remove as module just to delete the sub folder and all it files are removed. If a project has more 30 modules it will be hard to find the file (Its possible to search but should be nice just have it in same folder). I been working with Drupal before that has this type of module system where you can easy add and remove modules (Might be that am used to to it and not yet familiar with this new this way of doing it). What are the main benefits of the first approach over the module way?

Is this possible to achieve in the express framework? If so any tutorials on how to set it up? Any other node.js framework support this?


Solution

  • Yes its possible and even suggested by some to organize your files around features, not roles: https://blog.risingstack.com/node-hero-node-js-project-structure-tutorial/

    It might be common to do in smaller project like that use the express framework. However for bigger project that use react for example there its instead more common to split the code into components.