Skip to main content
Sosho Studio logo
Three transparent software architecture layers joined by a request pathway
Node.jsBackendPublished Updated

A Clean Node.js API Architecture

A proven structure for maintainable Node.js APIs: route, service, and repository layers, input validation, standard errors, logging, configuration, and testing.

Quick answer

A clean Node.js API has three separate layers: the route or controller only receives input, validates it, and returns the response; the service runs business logic; and the repository is the only place that talks to the database. Around them, a consistent error format, structured logs with a request id, validated configuration from environment variables, and tests for each layer make development and maintenance simple and predictable.

Separate the layers

The route or controller only receives input and returns a response. Business logic lives in services and database access in repositories. This separation means changing the database or the HTTP framework does not touch the core logic.

  • routes/: route definitions wired to controllers
  • services/: business rules with no HTTP dependency
  • repositories/: queries and data access
  • schemas/: input and output validation schemas
  • lib/: logging, configuration, errors, and shared utilities

To put this into practice, explore the scope and real work for these services: Custom Web App Development and Software Design & Architecture.

Validate at the edge

Check every request against a defined schema, for example with Zod, Joi, or JSON Schema, before it reaches a service: body, path parameters, and query. Reject unknown fields so nobody can tamper with data they should not change.

When the validation schema is also the source of types, service code works with trusted, typed data and you no longer re-check input in every function.

Standard, predictable errors

Build a central error middleware that maps domain errors such as not found or forbidden to the right HTTP status. Keep the error response format identical across the API; the Problem Details standard (RFC 9457) is a good choice.

Never return stack traces or internal database details to the client; record them only in logs, together with the request id.

Logging, monitoring, and tracing

Without good logs, finding a production bug is guesswork.

  • Structured JSON logs with a request id on every line
  • No passwords, tokens, or personal data in logs
  • Metrics such as latency and error rate per route
  • Distributed tracing, for example with OpenTelemetry, once you have several services

Configuration and dependencies

Read configuration from environment variables and validate it at startup, so a missing value stops the app immediately with a clear message instead of in the middle of a request.

Pass the repository into the service as a dependency instead of letting the service create it. This makes testing simple, because a fake repository can be swapped in. Do not forget graceful shutdown so in-flight requests are not cut off.

Testing at every layer

Test services with fake repositories in unit tests, routes with integration tests against a real database in a container, and the API contract with tests that check responses and errors. Tests should run automatically before every deployment.

Frequently asked questions

Express, Fastify, or NestJS?

All three can build a good API, and architecture matters more than the framework. Fastify is fast and light, Express is simple and widespread, and NestJS gives larger teams a more opinionated structure.

Monolith or microservices?

Most projects should start as a modular monolith. Once module boundaries and scaling needs are clear, splitting out an independent service is simpler and less risky.

How should we version an API?

The most common approach is a path prefix such as /v1. More important than the method is avoiding breaking changes: add new fields, never silently remove old ones, and announce deprecation timelines.