📖 Scenario: You are building a simple Express web server that manages a list of books. To keep your code clean and organized, you want to separate the route handling logic into a controller.
🎯 Goal: Create an Express server with a controller pattern where route handlers are defined in a separate controller object. You will set up the data, configure a controller, connect routes to controller methods, and complete the server setup.
📋 What You'll Learn
Create an array called books with three book objects, each having id and title properties
Create a controller object called bookController with a method getAllBooks that sends the books array as JSON
Set up an Express route GET /books that uses bookController.getAllBooks as the handler
Complete the Express server setup to listen on port 3000
💡 Why This Matters
🌍 Real World
Separating route logic into controllers helps keep Express apps organized and easier to maintain, especially as they grow.
💼 Career
Understanding the controller pattern is essential for backend developers working with Express or similar web frameworks.
Progress0 / 4 steps
1
DATA SETUP: Create the books array
Create an array called books with these exact objects: { id: 1, title: 'The Hobbit' }, { id: 2, title: '1984' }, and { id: 3, title: 'Pride and Prejudice' }.
Express
Hint
Use const books = [ ... ] with three objects inside the array.
2
CONFIGURATION: Create the bookController object
Create a constant object called bookController with a method getAllBooks that takes req and res parameters and sends the books array as JSON using res.json(books).
Express
Hint
Define bookController as an object with a method getAllBooks that calls res.json(books).
3
CORE LOGIC: Set up the Express route using the controller
Import Express, create an app with express(), and add a route GET /books that uses bookController.getAllBooks as the handler.
Express
Hint
Use const app = express() and app.get('/books', bookController.getAllBooks).
4
COMPLETION: Start the Express server
Add app.listen(3000) to start the server on port 3000.
Express
Hint
Use app.listen(3000) to start the server.
Practice
(1/5)
1. What is the main purpose of using the controller pattern in Express route handlers?
easy
A. To separate route logic from routing setup for better organization
B. To combine all route handlers into a single file
C. To directly write SQL queries inside route handlers
D. To avoid using middleware functions in routes
Solution
Step 1: Understand the controller pattern concept
The controller pattern means moving the logic that handles requests out of the route definitions into separate functions or files.
Step 2: Identify the benefit of separation
This separation improves code organization, making it easier to read and maintain.
Final Answer:
To separate route logic from routing setup for better organization -> Option A
Quick Check:
Controller pattern = separation of concerns [OK]
Hint: Controllers keep route files clean and logic separate [OK]
Common Mistakes:
Thinking controllers combine all routes in one file
C. Calling userController.getUser() immediately instead of passing function
D. Route path should be '/users' not '/user'
Solution
Step 1: Check how controller function is passed
The code calls userController.getUser() immediately, which runs the function and passes its result, not the function itself.
Step 2: Understand correct usage
Express expects a function reference as the route handler, so it should be userController.getUser without parentheses.
Final Answer:
Calling userController.getUser() immediately instead of passing function -> Option C
Quick Check:
Pass function reference, not call it [OK]
Hint: Don't add () when passing controller to route [OK]
Common Mistakes:
Calling controller function instead of passing it
Confusing res.json and res.send (both valid here)
Assuming route path naming causes error
5. You want to organize your Express app using the controller pattern. You have multiple routes for users: getUser, createUser, and deleteUser. Which approach best follows the controller pattern for clean code?
hard
A. Put all route definitions and logic inside a single routes.js file without controllers
B. Define all user-related functions in a separate userController.js file and import them in the router file
C. Write all user route handlers directly inside the main app.js file
D. Use inline anonymous functions for each route in the router file
Solution
Step 1: Understand controller pattern best practice
Controllers group related route logic into separate files or modules to keep code organized and reusable.
Step 2: Evaluate options for clean code
Defining all user-related functions in a dedicated userController.js file and importing them into the router file follows the pattern well.
Final Answer:
Define all user-related functions in a separate userController.js file and import them in the router file -> Option B
Quick Check:
Separate logic into controller files [OK]
Hint: Group related handlers in controller files, import in routes [OK]
Common Mistakes:
Putting all logic in main app file making it messy
Mixing routes and logic in one file without separation
Using anonymous functions inline reduces reusability
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.