Clean Architecture Making Node Js API Shine! by Michael Kibenko
Clean Architecture Making Node Js API Shine! by Michael Kibenko
Listen Share
Testability: We need to test our app using different types of tests, among them
unit, integration, functional, smoke, and e2e tests. The separation of elements
lends itself to easier testing, where we can test each module independently of
others, with specific injected properties.
Looking at the example below, found in many production projects, let’s analyze
the problematic aspects of this code.
Code scalability: Assuming we want to add validation of the ID parameter
before the Model execution, we will need to add the validation logic in the
request handler, and this will break the single responsibility principle because
the responsibility of the controller is to parse the request data and reply with a
relevant result.
Solution: Call a service layer that is responsible for processing this request.
Testability: In order to test this function, you will need to use some form of
mock DB and make a fake request using something like chai-http. The problem
here is that this test will be hard to initialize and it will take a long time to
execute.
The second problem here is that you can’t test every part of this code
independently.
Solution: This controller should call a service layer that will be responsible for
processing the request. Once you’ve done that, testing should be easy: just make
a mock service with your expected data responses and test the service logic
independently — thus you’ve decoupled the business logic from the cumbersome
to test fetching technology (in this case, HTTP).
Search Medium
Yellow layer: The Web server layer — including routes, will execute controllers.
Red layer: The Controllers layer, which will receive the users’ data and execute
the relevant use cases and compile a structured response.
Green layer: The use cases layer, will receive the user data and will process this
request including validation and external systems reporting (such as Kafka or
Kibana), which will receive the data from the entity repository layer as a source
of truth.
Blue layer: The entities layer, will include the entities and entities repositories —
only this layer will make DB queries, and access other services or third parties to
get data.
700 5
Code Dependency Rule: Code Dependencies can only move from the outer
levels inward. Code on the inner layers can have no knowledge of functions on
the outer layers. The variables, functions, and classes in the outer layers cannot
be mentioned in the more inward levels.
Response contracts: In order to make your API predictable, use the response
contract layer, which means that every response should be structured, including
errors.
If you want to see how to use the Response contract layer in practice, join my
course on udemy.com
Controller will receive the user’s requests and reply with a structured response.
Service will process the user’s request, including validations and third-party
reporting, and will receive data using the repository layer as a source of truth.
Repository will function as the application’s source of truth, will include the DB
and external services queries.
“Your architectures should tell readers about the system, not about the
frameworks you used in your system” — Robert C. Martin.
Using Typescript will boost your architectural skills, you from doing
architectural mistakes by providing type safety, code readability, modularity,
encapsulation, code maintainability, and a better developer experience. Use
Typescript!
Conclusions
Happy hacking! :)
Follow
Monolith vs Microservices vs Serverless and what to choose for your business needs
56 1
1.1K 13
583 8
45 1
Alessandro Traversi
6.1K 57
Lists
Samra Khan
71 1
Vijini Mallawaarachchi in Towards Data Science
39K 127
Sudharsanan Rajaram
1.8K 15