A basic Hapi.js API following Clean Architecture principles
Getting started (< 2mn)
git clone email@example.com:jbuget/nodejs-clean-architecture-app.git cd hapijs-v17-app npm install npm test npm start
In a browser, open http://localhost:3000/hello.
DDD and Clean Architecture
The application follows the Uncle Bob "Clean Architecture" principles and project structure :
Clean Architecture layers
Flow of Control
Server, Routes and Plugins
Server, routes and plugins can be considered as "plumbery-code" that exposes the API to the external world, via an instance of Hapi.js server.
The role of the server is to intercept the HTTP request and match the corresponding route.
Routes are configuration objects whose responsibilities are to check the request format and params, and then to call the good controller (with the received request).
Plugins are configuration object that package an assembly of features (ex: authentication & security concerns, routes, pre-handlers, etc.) and are registered at the server startup.
Controllers (a.k.a Route Handlers)
Controllers are the entry points to the application context.
They have 3 main responsibilities :
- Extract the parameters (query or body) from the request
- Call the good Use Case (application layer)
- Return an HTTP response (with status code and serialized data)
A use case is a business logic unit.
It is a class that must have an
execute method which will be called by controllers.
It may have a constructor to define its dependencies (concrete implementations - a.k.a. adapters - of the port objects) or its execution context.
Be careful! A use case must have only one precise business responsibility!
A use case can call objects in the same layer (such as data repositories) or in the domain layer.