Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Why REST principles matter
📖 Scenario: You are building a simple web server using Express.js to handle requests for a small online bookstore. You want to organize your routes and responses following REST principles to make your API easy to understand and maintain.
🎯 Goal: Build a basic Express.js server with RESTful routes for books. You will create the initial data, set up a configuration variable, implement RESTful GET and POST routes, and complete the server setup.
📋 What You'll Learn
Create an array of book objects with exact titles and authors
Add a configuration variable for the server port
Implement RESTful GET and POST routes for books
Complete the Express server setup with middleware and listen on the port
💡 Why This Matters
🌍 Real World
RESTful APIs are used everywhere to connect web and mobile apps to servers. Understanding REST principles helps you build APIs that other developers can easily use and maintain.
💼 Career
Many software development jobs require building or working with REST APIs. Knowing how to structure routes and handle requests with Express.js is a valuable skill for backend development.
Progress0 / 4 steps
1
DATA SETUP: Create the initial books array
Create an array called books with these exact objects: { id: 1, title: 'The Hobbit', author: 'J.R.R. Tolkien' } and { id: 2, title: '1984', author: 'George Orwell' }.
Express
Hint
Use const books = [ ... ] with two objects inside.
2
CONFIGURATION: Set the server port
Create a constant called PORT and set it to 3000.
Express
Hint
Use const PORT = 3000; to set the port number.
3
CORE LOGIC: Implement RESTful GET and POST routes
Import Express, create an app with express(), add JSON middleware with app.use(express.json()), then create a GET route at /books that sends the books array, and a POST route at /books that adds a new book from req.body to books and sends the new book.
Express
Hint
Use express() to create the app, add JSON middleware, then define GET and POST routes for /books.
4
COMPLETION: Start the server listening on the port
Add app.listen(PORT, () => { console.log(`Server running on port ${PORT}`); }) to start the server.
Express
Hint
Use app.listen(PORT, () => { console.log(...); }) to start the server.
Practice
(1/5)
1. Why is it important to follow REST principles when building an Express API?
easy
A. It requires using XML instead of JSON for data exchange.
B. It forces the use of only POST requests for all actions.
C. It makes the API easier to understand and use by clients.
D. It limits the API to only one endpoint for all resources.
Solution
Step 1: Understand REST principles
REST encourages clear use of HTTP methods and resource URLs to make APIs intuitive.
Step 2: Connect REST to client ease
Following REST helps clients know how to interact with the API without confusion.
Final Answer:
It makes the API easier to understand and use by clients. -> Option C
Quick Check:
REST improves API clarity = A [OK]
Hint: REST means clear, simple API design for clients [OK]
Common Mistakes:
Thinking REST requires only POST requests
Believing REST forces XML data format
Assuming REST limits to one endpoint
2. Which HTTP method should you use in Express to retrieve data from a resource following REST principles?
easy
A. GET
B. PUT
C. DELETE
D. POST
Solution
Step 1: Recall HTTP methods in REST
GET is used to retrieve or read data without changing it.
Step 2: Match method to action
Since retrieving data means reading, GET is the correct method.
Final Answer:
GET -> Option A
Quick Check:
Retrieve data = GET [OK]
Hint: GET is for reading data, POST for creating [OK]
Common Mistakes:
Using POST to fetch data
Confusing DELETE with data retrieval
Using PUT instead of GET for reading
3. Consider this Express route following REST principles:
Why might this not follow REST principles correctly?
medium
A. The URL should not include the user id.
B. POST should not be used for updating existing resources.
C. The response message is incorrect.
D. The route should use GET instead of POST.
Solution
Step 1: Understand REST method usage
POST is usually for creating new resources, while PUT or PATCH is for updating existing ones.
Step 2: Identify correct method for update
Updating user should use PUT or PATCH, not POST, to follow REST.
Final Answer:
POST should not be used for updating existing resources. -> Option B
Quick Check:
Update uses PUT/PATCH, not POST [OK]
Hint: Use PUT/PATCH to update, not POST [OK]
Common Mistakes:
Thinking POST is for updates
Ignoring URL with resource id
Confusing GET with update
5. You want to design an Express API that follows REST principles for managing books. Which of these URL and method combinations correctly follows REST for updating a book's details?
hard
A. DELETE /books/:id
B. POST /books/update
C. GET /books/:id/update
D. PUT /books/:id
Solution
Step 1: Identify REST method for updating
PUT is the standard HTTP method to update an existing resource fully.
Step 2: Check URL pattern for resource
Using /books/:id targets a specific book by its id, which is correct REST style.
Step 3: Evaluate other options
POST /books/update is not RESTful because it uses a verb in URL; GET is for reading, DELETE is for removal.
Final Answer:
PUT /books/:id -> Option D
Quick Check:
Update book = PUT /books/:id [OK]
Hint: Update uses PUT with resource ID in URL [OK]