📖 Scenario: You are building a simple Express app that manages a list of books. To keep your code clean and organized, you will use the repository pattern to handle data access. This means you will create a separate module that manages the books data, so your main app code stays simple and easy to maintain.
🎯 Goal: Build a repository module that stores books data and provides methods to get all books and add a new book. Then use this repository in an Express route to show all books.
📋 What You'll Learn
Create a books array with initial book objects
Create a BookRepository object with methods to get all books and add a book
Use the repository methods inside an Express route to send the list of books as JSON
Set up a basic Express server listening on port 3000
💡 Why This Matters
🌍 Real World
The repository pattern helps keep your Express app organized by separating data access from route handling. This makes your code easier to maintain and test.
💼 Career
Many backend jobs require clean code organization. Using repository pattern is a common practice to manage data access in Node.js and Express applications.
Progress0 / 4 steps
1
DATA SETUP: Create the initial books data array
Create a variable called books that is an array containing these two book objects exactly: { id: 1, title: 'The Hobbit' } and { id: 2, title: '1984' }.
Express
Hint
Use const books = [ ... ] and include the two objects inside the array.
2
CONFIGURATION: Create the BookRepository object
Create a constant called BookRepository that is an object with two methods: getAll which returns the books array, and add which takes a book parameter and pushes it into the books array.
Express
Hint
Define BookRepository as an object with two functions: getAll and add.
3
CORE LOGIC: Use the repository in an Express route
Create an Express app by requiring express and calling express(). Then add a GET route on '/books' that uses BookRepository.getAll() to get all books and sends them as JSON with res.json().
Express
Hint
Use require('express') and express() to create the app. Then add a GET route on /books that sends JSON from the repository.
4
COMPLETION: Start the Express server on port 3000
Add a line to start the Express app listening on port 3000 using app.listen(3000).
Express
Hint
Use app.listen(3000) to start the server on port 3000.
Practice
(1/5)
1. What is the main purpose of using the Repository pattern in an Express app?
easy
A. To manage user authentication
B. To handle HTTP requests and responses
C. To style the frontend components
D. To separate data access logic from business logic
Solution
Step 1: Understand the role of repository pattern
The repository pattern is designed to isolate data access logic from the rest of the application.
Step 2: Identify the correct purpose in Express context
In Express apps, it helps keep business logic clean by moving database queries into repositories.
Final Answer:
To separate data access logic from business logic -> Option D
Quick Check:
Repository pattern = separate data access [OK]
Hint: Repository pattern means separate data access from app logic [OK]
Common Mistakes:
Confusing repository with routing or middleware
Thinking it handles UI or styling
Mixing it with authentication logic
2. Which of the following is the correct way to define a repository class in Express using ES6 syntax?
easy
A. class UserRepository { getAll() { /* code */ } }
B. var UserRepository = new Object({ getAll: function() {} })