ReviseAlgo Logo

TypeScript with Node.js

Building a REST API with TypeScript

Architect a scalable, layered REST API with Express, Controllers, Services, and Zod validation.

Last Updated: July 29, 2026 10 min read

Building production REST APIs requires structured layering: Controllers handle HTTP requests, Services manage business logic, and DTOs/Schemas handle validation.

1. Introduction & Architecture

2. Deep Dive & Core Concepts

Separating controllers from services ensures business logic remains testable independently of Express HTTP req and res objects.

3. Basic Code Example

4. Advanced Production Patterns

5. Interactive Code Playground

console.log("Layered REST API architecture ready.");

6. Common Pitfalls & Edge Cases

Note: Wrap async express route handlers with an asyncHandler or use Express 5 to ensure unhandled promise rejections reach global error middleware automatically.

7. Interview Q&A & Quizzes

Q: Why separate Controllers and Services in REST API design?

A: Controllers deal with HTTP concerns (status codes, headers, req/res parsing), while Services encapsulate framework-agnostic business logic.

8. Summary Comparison Table

Architecture LayerResponsibilityExample Tech
RouterEndpoint path mappingExpress Router
ControllerHTTP Request/Response handlingExpress Controllers
ServiceCore domain & business rulesPlain TS Classes
| Database | Data persistence | Prisma / Drizzle |