Project Structures

Choose the architecture that best fits your project needs. SargenJS supports two main architectural patterns.

Layered Architecture

Traditional MVC (Model-View-Controller) pattern with clear separation of concerns across layers.

Structure:

my-project/
├── src/
│ ├── routes/
│ │ └── index.js
│ ├── controllers/
│ │ └── userController.js
│ ├── services/
│ │ └── userService.js
│ └── models/
│ └── User.js
├── app.js
├── .env
└── package.json

Best for:

  • • Traditional web applications
  • • Teams familiar with MVC pattern
  • • Projects with clear business logic separation
  • • CRUD-heavy applications

Modular Architecture

Feature-based organization where each module contains all related functionality.

Structure:

my-project/
├── src/
│ ├── config/
│ │ └── database.js
│ ├── common/
│ │ ├── middleware/
│ │ └── utils/
│ └── modules/
│ └── users/
│ ├── routes.js
│ ├── controller.js
│ ├── service.js
│ └── model.js
├── app.js
├── .env
└── package.json

Best for:

  • • Microservices architecture
  • • Large-scale applications
  • • Team-based development
  • • Feature-driven development

Detailed Comparison

AspectLayeredModular
OrganizationBy technical layerBy business feature
ScalabilityGood for small-medium projectsExcellent for large projects
Team CollaborationLayer-based teamsFeature-based teams
Code ReusabilityModerateHigh
Learning CurveFamiliar MVC patternRequires understanding of modules
MaintenanceEasy for small projectsBetter for complex projects

File Organization Details

Layered Structure

src/routes/

Contains route definitions and URL mappings. Routes handle HTTP requests and delegate to controllers.

src/controllers/

Business logic handlers. Controllers process requests, call services, and return responses.

src/services/

Core business logic and data processing. Services contain reusable business operations.

src/models/

Data models and database schemas. Models define data structure and relationships.

Modular Structure

src/config/

Application configuration files. Database connections, environment settings, etc.

src/common/

Shared utilities, middleware, and common functionality used across modules.

src/modules/

Feature-based modules. Each module contains its own routes, controllers, services, and models.

Module Structure

Each module is self-contained with its own routes, controller, service, and model files.

Choosing the Right Structure

Choose Layered When:

  • • Building a traditional web application
  • • Team is familiar with MVC pattern
  • • Project size is small to medium
  • • Clear separation of concerns is needed
  • • Rapid prototyping is required

Choose Modular When:

  • • Building a large-scale application
  • • Multiple teams working on different features
  • • Planning to extract modules as microservices
  • • High code reusability is important
  • • Feature-driven development approach

Migration Between Structures

While SargenJS doesn't provide automatic migration between structures, you can manually reorganize your code:

Layered to Modular:

Group related controllers, services, and models into feature modules
Move shared utilities to src/common/
Create module-specific route files

Modular to Layered:

Extract all controllers to src/controllers/
Consolidate services in src/services/
Move models to src/models/