Designing a Scalable Folder Structure for Our Node.js E-commerce API (Clean Architecture + Feature-Based)

March 11, 2026 ยท 8 min read

Hey folks! ๐Ÿ‘‹

In the last post, we saw how messy traditional code gets when everything (HTTP, business rules, DB) is thrown together. We fixed that with Clean Architecture layers.

But layers alone aren't enough when your app grows to 20+ features. Scrolling through giant controllers/ or services/ folders becomes a nightmare.

The fix? Organize by feature (also called vertical slices or feature-first), while keeping Clean Architecture inside each feature.

Goal Recap

Step 1: Identify the Core Business Features

E-commerce revolves around clear business capabilities:

We group code by these features instead of by type.

src/
โ””โ”€โ”€ features/
    โ”œโ”€โ”€ users/
    โ”œโ”€โ”€ products/
    โ”œโ”€โ”€ cart/
    โ”œโ”€โ”€ orders/
    โ”œโ”€โ”€ payments/
    โ””โ”€โ”€ reviews/

Want to work on "products"? Just open src/features/products โ€” everything is right there. No more hunting across folders!

Step 2: Apply Clean Architecture Inside Each Feature

Inside a feature folder, we still separate concerns using Clean layers:

src/
โ””โ”€โ”€ features/
    โ””โ”€โ”€ users/
        โ”œโ”€โ”€ domain/          โ† Core business concepts (pure JS/TS, no frameworks!)
        โ”œโ”€โ”€ application/     โ† Use cases / business flows
        โ”œโ”€โ”€ infrastructure/  โ† DB, external services (Mongo, email, etc.)
        โ””โ”€โ”€ presentation/    โ† API controllers, routes, validation

Domain Layer โ€” The Heart โค๏ธ

Pure business rules. No Express, no MongoDB imports.

Contains:

domain/
โ”œโ”€โ”€ entities/
โ”‚   โ””โ”€โ”€ User.js
โ””โ”€โ”€ repositories/
    โ””โ”€โ”€ IUserRepository.js   // โ† just the contract!

Rule: This layer never depends on outer layers or tech. It stays stable forever.

Application Layer โ€” The Brain ๐Ÿง 

Use cases โ€” what the system can actually do.

Examples: RegisterUser, LoginUser, UpdateProfile, AddAddress

application/
โ””โ”€โ”€ use-cases/
    โ”œโ”€โ”€ registerUser.js
    โ”œโ”€โ”€ loginUser.js
    โ””โ”€โ”€ updateProfile.js

These coordinate domain entities + call repositories (via interfaces).

Infrastructure Layer โ€” The Tools ๐Ÿ”ง

Concrete implementations of domain interfaces.

infrastructure/
โ”œโ”€โ”€ repositories/
โ”‚   โ””โ”€โ”€ MongoUserRepository.js   // implements IUserRepository
โ””โ”€โ”€ services/
    โ””โ”€โ”€ JwtService.js

Switch DB from Mongo โ†’ PostgreSQL? Only change files here. Domain & application stay untouched!

Presentation Layer โ€” The Doorway ๐Ÿšช

Handles HTTP (Express routes/controllers).

presentation/
โ”œโ”€โ”€ controllers/
โ”‚   โ””โ”€โ”€ userController.js
โ”œโ”€โ”€ routes/
โ”‚   โ””โ”€โ”€ userRoutes.js
โ””โ”€โ”€ dtos/                  (optional: request/response shapes)
    โ””โ”€โ”€ registerUser.dto.js

It calls application use cases and returns responses.

Full Example: Users Feature Folder

src/
โ””โ”€โ”€ features/
    โ””โ”€โ”€ users/
        โ”œโ”€โ”€ domain/
        โ”‚   โ”œโ”€โ”€ entities/
        โ”‚   โ”‚   โ””โ”€โ”€ User.js
        โ”‚   โ””โ”€โ”€ repositories/
        โ”‚       โ””โ”€โ”€ IUserRepository.js
        โ”œโ”€โ”€ application/
        โ”‚   โ””โ”€โ”€ use-cases/
        โ”‚       โ”œโ”€โ”€ registerUser.js
        โ”‚       โ”œโ”€โ”€ loginUser.js
        โ”‚       โ””โ”€โ”€ updateProfile.js
        โ”œโ”€โ”€ infrastructure/
        โ”‚   โ”œโ”€โ”€ repositories/
        โ”‚   โ”‚   โ””โ”€โ”€ MongoUserRepository.js
        โ”‚   โ””โ”€โ”€ services/
        โ”‚       โ””โ”€โ”€ JwtService.js
        โ””โ”€โ”€ presentation/
            โ”œโ”€โ”€ controllers/
            โ”‚   โ””โ”€โ”€ userController.js
            โ”œโ”€โ”€ routes/
            โ”‚   โ””โ”€โ”€ userRoutes.js
            โ””โ”€โ”€ dtos/
                โ””โ”€โ”€ registerUser.dto.js

Repeat this pattern for products/, orders/, etc.

Classic Clean Architecture onion diagram by Uncle Bob

Classic Clean Architecture (onion) โ€” dependencies point inward

Vertical slice vs layered architecture comparison

Vertical slices (feature-based) vs traditional horizontal layers โ€” features group everything vertically

Why This Combo Rocks for E-commerce

This is a pragmatic blend of Clean Architecture (for inner stability) + Vertical Slice (for outer organization). Many production Node.js e-commerce APIs use exactly this.