Here’s an example. We’ll use Express web framework:

Suppose we’re building a quiz game where the users can submit their answer to a question. The answer will be submitted through an HTTP POST request. All data is stored inside a MongoDB database.

If the answer is correct, we’ll award the point to the user.

If the user finished answering all questions, thus cleared the game, we want to send an email congratulating the user.

If the answer is not correct, then we’ll need to tell the user to try again.

Here’s one way to implement it in Express.

Here, we’ve separated the backend code into multiple services. Each service has a responsibility in its own domain. In above example, we have: AnswerService, EmailService, PointsService, and QuestionService.

Although the above code is very straightforward, and we also created service modules to separate the concerns, in my opinion, it’s still not clean.

The above code that is supposed to describe a business logic also contains too much technical details which are irrelevant to the business use case, such as:

where the data came from (req.user, req.params, req.body).

where to find the needed data (QuestionService, AnswerService, …)

how to award points to a user (PointsService)

how to send the result back to the user (res.render, EmailService)

the HTML templates to use (answer-correct, answer-incorrect)

how to determine if an answer is correct (.trim().toLowerCase())

The business use-case is hardwired to the framework and service modules, summarized using this diagram: