0
0
Expressframework~10 mins

Service layer pattern in Express - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Service layer pattern
Client Request
Controller receives request
Controller calls Service Layer
Service Layer processes logic
Service Layer calls Data Access Layer
Data Access Layer interacts with DB
Data returned to Service Layer
Service Layer returns result to Controller
Controller sends response to Client
The service layer acts as a middleman between controllers and data access, handling business logic and keeping code organized.
Execution Sample
Express
app.get('/users/:id', async (req, res) => {
  const user = await userService.getUserById(req.params.id);
  res.json(user);
});
Controller receives a request, calls the service layer to get user data, then sends it back as JSON.
Execution Table
StepActionInputService Layer CallService ResultController Response
1Receive GET /users/42id=42Call getUserById(42)PendingPending
2Service queries DB for user 42id=42DB query executed{ id: 42, name: 'Alice' }Pending
3Service returns user datauser dataReturn { id: 42, name: 'Alice' }{ id: 42, name: 'Alice' }Pending
4Controller sends JSON responseuser dataNo callNo changeResponse: { id: 42, name: 'Alice' }
5Request completeN/AN/AN/AResponse sent to client
💡 Request completes after controller sends user data response
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
req.params.idundefined4242424242
userundefinedundefinedPending{ id: 42, name: 'Alice' }{ id: 42, name: 'Alice' }{ id: 42, name: 'Alice' }
Key Moments - 2 Insights
Why does the controller not access the database directly?
The controller calls the service layer (see execution_table step 1 and 3) to keep business logic separate and make code easier to maintain.
What happens if the service layer takes time to get data?
The controller waits asynchronously (step 1 shows 'Pending') until the service returns data before sending a response.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the service layer doing?
AQuerying the database for user data
BSending response to client
CReceiving client request
DReturning data to controller
💡 Hint
Check the 'Service Layer Call' and 'Service Result' columns at step 2
At which step does the controller send the response to the client?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'Controller Response' column for when the response is sent
If the service layer returned null instead of user data, what would change in the execution_table?
AController Response at step 4 would be unchanged
BService Result at step 3 would be null
CRequest would complete at step 2
DDB query would not happen
💡 Hint
Focus on the 'Service Result' column at step 3
Concept Snapshot
Service Layer Pattern in Express:
- Controller handles HTTP requests
- Calls Service Layer for business logic
- Service Layer calls Data Access Layer (DB)
- Keeps code organized and testable
- Controller sends response after service returns data
Full Transcript
In Express, the service layer pattern separates concerns by having controllers handle requests and responses, while the service layer contains business logic and data access calls. When a client requests user data, the controller calls the service layer method getUserById with the user ID. The service layer queries the database and returns the user object. The controller then sends this data as a JSON response. This pattern keeps code clean and easier to maintain by isolating responsibilities.