0
0
Expressframework~20 mins

Service layer pattern in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Service Layer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What does this Express service layer return?
Consider this service function called by a route handler. What will be the resolved value when calling getUserById(2)?
Express
const users = [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}];

function getUserById(id) {
  return new Promise((resolve) => {
    const user = users.find(u => u.id === id);
    resolve(user ? user.name : null);
  });
}
A"Bob"
B"{ id: 2, name: 'Bob' }"
Cnull
Dundefined
Attempts:
2 left
💡 Hint
Look at what the promise resolves with: the user's name or the whole user object.
state_output
intermediate
1:30remaining
What is the final state of req.user after middleware and service call?
Given this Express middleware and service call, what will req.user contain after next()?
Express
const users = [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}];

function userService(id) {
  return users.find(u => u.id === id) || null;
}

function authMiddleware(req, res, next) {
  req.user = userService(1);
  next();
}
A{ id: 2, name: 'Bob' }
Bnull
Cundefined
D{ id: 1, name: 'Alice' }
Attempts:
2 left
💡 Hint
Check what userService returns for id 1.
📝 Syntax
advanced
2:00remaining
Which option correctly implements an async service function in Express?
You want to create a service function that fetches user data asynchronously. Which option is syntactically correct and returns a promise resolving to the user object?
Express
const users = [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}];
Aasync function getUser(id) { return users.find(u => u.id === id); }
Bfunction getUser(id) { return new Promise(resolve => resolve(users.find(u => u.id === id))); }
Casync function getUser(id) { const user = users.find(u => u.id === id); return user; }
Dfunction getUser(id) { return await users.find(u => u.id === id); }
Attempts:
2 left
💡 Hint
Remember that Array.find is synchronous and does not return a promise.
🔧 Debug
advanced
2:00remaining
Why does this Express service call cause an unhandled promise rejection?
Examine the code below. Why does calling getUserById(3) cause an unhandled promise rejection?
Express
const users = [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}];

function getUserById(id) {
  return new Promise((resolve, reject) => {
    const user = users.find(u => u.id === id);
    if (!user) reject('User not found');
    else resolve(user);
  });
}

getUserById(3).then(user => console.log(user));
ABecause the promise rejects but there is no catch handler to handle the rejection.
BBecause users.find throws an error when user is not found.
CBecause resolve is called twice causing a conflict.
DBecause the function does not return a promise.
Attempts:
2 left
💡 Hint
Check how the promise rejection is handled after calling getUserById.
🧠 Conceptual
expert
1:30remaining
What is the main benefit of using a service layer in Express apps?
Why do developers use a service layer between Express route handlers and data access logic?
ATo replace middleware functions with simpler code.
BTo speed up HTTP requests by caching all responses automatically.
CTo separate business logic from routing logic, making code easier to maintain and test.
DTo directly connect route handlers to the database without abstraction.
Attempts:
2 left
💡 Hint
Think about code organization and responsibilities.