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
HTTP methods for CRUD operations
📖 Scenario: You are building a simple Express server to manage a list of books. Each book has an id and a title. You want to allow users to create, read, update, and delete books using HTTP methods.
🎯 Goal: Create an Express app that supports CRUD operations on a books array using the correct HTTP methods: GET, POST, PUT, and DELETE.
📋 What You'll Learn
Create an array called books with two book objects having id and title.
Add a variable called nextId to track the next book ID.
Create a GET route at /books to return all books.
Create a POST route at /books to add a new book with a unique id.
Create a PUT route at /books/:id to update the title of a book by id.
Create a DELETE route at /books/:id to remove a book by id.
💡 Why This Matters
🌍 Real World
Web servers often use HTTP methods to manage data resources like users, products, or posts. This project shows how to build a simple API for managing books.
💼 Career
Understanding HTTP methods and Express routing is essential for backend web development jobs, API design, and full-stack roles.
Progress0 / 4 steps
1
Create the initial books array
Create an array called books with two objects: { id: 1, title: '1984' } and { id: 2, title: 'Brave New World' }.
Express
Hint
Use const books = [ ... ] with two objects inside.
2
Add a nextId variable
Add a variable called nextId and set it to 3 to track the next book ID.
Express
Hint
Use let nextId = 3; to allow incrementing later.
3
Create GET and POST routes
Create a GET route at /books that sends the books array as JSON. Then create a POST route at /books that adds a new book with a unique id from nextId and increments nextId. Use req.body.title for the new book's title.
Express
Hint
Use app.get('/books', (req, res) => { res.json(books); }) and app.post('/books', (req, res) => { ... }).
4
Create PUT and DELETE routes
Create a PUT route at /books/:id that updates the title of the book with the matching id from req.params.id. Then create a DELETE route at /books/:id that removes the book with the matching id. Use res.status(204).end() after deletion.
Express
Hint
Use app.put('/books/:id', ...) to find and update the book, and app.delete('/books/:id', ...) to remove it.
Practice
(1/5)
1. Which HTTP method is typically used in Express to retrieve data from a server?
easy
A. PUT
B. POST
C. DELETE
D. GET
Solution
Step 1: Understand HTTP methods purpose
GET is used to read or retrieve data from the server without changing it.
Step 2: Match method to action
Since the question asks for retrieving data, GET is the correct method.
Final Answer:
GET -> Option D
Quick Check:
Retrieve data = GET [OK]
Hint: GET always reads data without changing it [OK]
Common Mistakes:
Confusing POST with GET for reading data
Using DELETE to get data
Thinking PUT retrieves data
2. Which of the following is the correct Express syntax to handle a POST request to the path /users?
easy
A. app.get('/users', (req, res) => { ... })
B. app.put('/users', (req, res) => { ... })
C. app.post('/users', (req, res) => { ... })
D. app.delete('/users', (req, res) => { ... })
Solution
Step 1: Identify method for creating data
POST is used to create new data on the server.
Step 2: Match Express syntax to POST
app.post('/users', ...) correctly handles POST requests to /users.
Final Answer:
app.post('/users', (req, res) => { ... }) -> Option C
Quick Check:
POST creates data = app.post() [OK]
Hint: Use app.post() for creating new data routes [OK]
Common Mistakes:
Using app.get() for POST requests
Confusing app.put() with app.post()
Writing app.delete() for creation
3. What will be the response status code if you define this Express route and send a request to PUT /items/5?