Back to blog

2026-11-27

Structuring Next.js App Router with OOP & MVC Boundaries

How we keep Next.js route handlers thin and testable by separating schemas, services, and controllers.

nextjs · architecture · typescript

When a Next.js App Router project grows beyond a few pages, mixing database queries, Zod validation, and HTTP responses inside route.ts quickly becomes hard to maintain.

Clear Layer Boundaries

At Nurabyte, we organize server-side code into three focused layers under src/server/:

  • Schemas (src/server/schemas/): Zod contracts defining request validation and inferred TypeScript types.
  • Services (src/server/services/): OOP classes (InquiryService, BlogService) encapsulating database or filesystem access.
  • Controllers (src/server/controllers/): HTTP orchestration that parses requests, validates inputs, delegates to services, and returns a consistent ApiResponse<T> envelope.

Why It Pays Off

  1. Validation and business logic can be unit tested directly with node --test without booting a Next.js server.
  2. Route handlers stay 5–10 lines long and easy to audit.
  3. Server-only secrets (SUPABASE_SERVICE_ROLE_KEY) remain isolated from Client Components.