0
0
Expressframework~10 mins

Service layer pattern in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the service layer module correctly.

Express
const userService = require('[1]');
Drag options to blanks, or click blank then click option'
A./services/userService
Bexpress
Chttp
Dpath
Attempts:
3 left
💡 Hint
Common Mistakes
Importing core modules like 'express' instead of the service file.
2fill in blank
medium

Complete the code to call the service method that fetches all users.

Express
app.get('/users', async (req, res) => {
  const users = await userService.[1]();
  res.json(users);
});
Drag options to blanks, or click blank then click option'
Ause
Blisten
Csend
DgetAllUsers
Attempts:
3 left
💡 Hint
Common Mistakes
Using Express app methods like 'listen' or 'use' instead of service methods.
3fill in blank
hard

Fix the error in the service method call to get a user by ID.

Express
const user = await userService.getUserById([1]);
Drag options to blanks, or click blank then click option'
Ares.send
Breq.body
Creq.params.id
Dapp.get
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the whole request body or response methods instead of the ID.
4fill in blank
hard

Fill both blanks to create a service method that updates a user and returns the updated user.

Express
async function updateUser([1], [2]) {
  const updatedUser = await UserModel.findByIdAndUpdate(id, data, { new: true });
  return updatedUser;
}
Drag options to blanks, or click blank then click option'
Aid
Bdata
Creq
Dres
Attempts:
3 left
💡 Hint
Common Mistakes
Using request or response objects directly in service methods.
5fill in blank
hard

Fill all three blanks to create a service method that deletes a user by ID and returns a confirmation message.

Express
async function deleteUser([1]) {
  await UserModel.findByIdAndDelete([2]);
  return { message: `User with ID [3] deleted.` };
}
Drag options to blanks, or click blank then click option'
Aid
C${id}
DuserId
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently or missing template literal syntax.