DEV Community

codeagboro
codeagboro

Posted on

Folder Arrangement in a Node Js application

Organizing your codebase in Node.js can be a challenge, depending on the specific needs of your project. Fortunately, there are some common folder structures that make it easier to maintain and keep tidy. There are several structures that can be employed in arranging folders for a node js application. Below is a list of a few of them however it is worthy of note that folder arrangement is based on the needs of your project.

MVC Structure:

MVC is a widely used software architecture for web applications. It divides the program into three parts: Model, View and Controller. We can use this structure to organize our Node.js folder structure. For instance, here's an example of how to do it:

- project/
  - controllers/
    - usersController.js
    - productsController.js
  - models/
    - userModel.js
    - productModel.js
  - views/
    - users/
      - index.ejs
      - profile.ejs
    - products/
      - index.ejs
      - details.ejs
  - public/
    - css/
      - style.css
    - js/
      - script.js
  - app.js
  - index.js
  - package.json

Enter fullscreen mode Exit fullscreen mode

This structure consists of a controllers/ folder that holds the application's controllers. Every controller deals with user requests and collaborates with the model to get or update data. The models/ folder contains the models for the application, which handle data storage and retrieval. The views/ folder has the views of the app, that are responsible for displaying HTML to the user. Moreover, inside public/ there are static assets such as CSS and JavaScript files. Lastly, index.js acts as the main entry point for the application, where it connects controllers, models, and views together.

Node.js's MVC pattern assists in isolating concerns, allowing code to be more modular and maintainable. Controllers, models, and views can all be developed separately, thus facilitating testing and debugging. Moreover, it facilitates a cleaner separation of responsibilities, wherein each application component is responsible for a particular set of functions, making the code easier to comprehend.

However in a typical MVC structure we can also have routes and config folders seperately
The routes/ folder holds the application's routes, which define the endpoints a user can use. These routes handle receiving HTTP requests, connecting to the appropriate controller function and delivering back an HTTP response. As part of the MVC structure, these routes act as part of the Controller layer, as they control how data flows between an individual and the application.

The config/ folder is home to configuration files like environment variables, database settings and authentication settings. In MVC structure, these configuration files are part of the Model layer since they manage the storage and retrieval of data for the application.
So, the config/ and routes/ folders can be included in the MVC folder structure like this:

- src/
  - controllers/
    - usersController.js
    - productsController.js
  - models/
    - userModel.js
    - productModel.js
  - routes/
    - users.js
    - products.js
  - views/
    - users/
      - index.ejs
      - profile.ejs
    - products/
      - index.ejs
      - details.ejs
  - config/
    - env.js
    - database.js
    - auth.js
  - public/
    - css/
      - style.css
    - js/
      - script.js
  - app.js
 - index.js
  - package.json
Enter fullscreen mode Exit fullscreen mode

Arrangement based on features

Another common pattern is to organize the project based on the feature or module. In this pattern, each feature or module has its own folder that contains all the necessary files for that feature or module. For example:

- project/
  - features/
    - users/
      - index.js
      - usersController.js
      - userModel.js
      - user.ejs
      - user.css
      - user.js
    - products/
      - index.js
      - productsController.js
      - productModel.js
      - product.ejs
      - product.css
      - product.js
  - app.js
  - package.json
Enter fullscreen mode Exit fullscreen mode

There are other patterns as well, such as organizing by layers (presentation, application, domain, infrastructure), by file type (controllers, models, views, helpers), or by use case. Ultimately, the folder structure you choose should be based on the needs of your project and your personal preference.

Top comments (1)

Collapse
 
unyimedc profile image
Unyimedc

awesome and indeed helpful