REST principles help make web services simple, fast, and easy to use. They guide how servers and clients talk clearly and predictably.
Why REST principles matter in Express
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Express
GET /resources - to read data POST /resources - to create data PUT /resources/:id - to update data DELETE /resources/:id - to delete data
Use HTTP methods (GET, POST, PUT, DELETE) to represent actions clearly.
Use URLs to represent resources (things) instead of actions.
Examples
Express
GET /books POST /books PUT /books/123 DELETE /books/123
Express
GET /users/45/orders POST /users/45/orders
Sample Program
This Express app follows REST principles by using HTTP methods and clear URLs to manage books. It shows how clients can get, add, update, and delete books easily.
Express
import express from 'express'; const app = express(); app.use(express.json()); // Sample data let books = [{ id: 1, title: 'Learn REST' }]; // Get all books app.get('/books', (req, res) => { res.json(books); }); // Add a new book app.post('/books', (req, res) => { const newBook = { id: books.length + 1, title: req.body.title }; books.push(newBook); res.status(201).json(newBook); }); // Update a book app.put('/books/:id', (req, res) => { const id = Number(req.params.id); const book = books.find(b => b.id === id); if (!book) return res.status(404).send('Book not found'); book.title = req.body.title; res.json(book); }); // Delete a book app.delete('/books/:id', (req, res) => { const id = Number(req.params.id); books = books.filter(b => b.id !== id); res.status(204).send(); }); app.listen(3000, () => console.log('Server running on http://localhost:3000'));
Important Notes
Following REST makes your API predictable and easier for others to use.
Use proper HTTP status codes to tell clients what happened (like 201 for created, 404 for not found).
Keep URLs simple and focused on resources, not actions.
Summary
REST principles help build clear and easy-to-use web services.
Use HTTP methods and resource URLs to communicate actions and data.
Following REST improves maintainability and client compatibility.
Practice
1. Why is it important to follow REST principles when building an Express API?
easy
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 CQuick 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
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 AQuick 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:
What HTTP method and URL would a client use to delete user with id 5?
app.delete('/users/:id', (req, res) => {
// delete user logic
res.send('User deleted');
});What HTTP method and URL would a client use to delete user with id 5?
medium
Solution
Step 1: Identify the route method and path
The route uses app.delete with path '/users/:id', so it expects DELETE requests to /users/{id}.Step 2: Match method and URL for deleting user 5
To delete user 5, client sends DELETE request to /users/5.Final Answer:
DELETE /users/5 -> Option AQuick Check:
Delete user = DELETE /users/5 [OK]
Hint: Delete uses DELETE method with resource URL [OK]
Common Mistakes:
- Using GET or POST to delete
- Confusing PUT with DELETE
- Using wrong URL path
4. You wrote this Express route to update a user:
Why might this not follow REST principles correctly?
app.post('/users/:id', (req, res) => {
// update user logic
res.send('User updated');
});Why might this not follow REST principles correctly?
medium
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 BQuick 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
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 DQuick Check:
Update book = PUT /books/:id [OK]
Hint: Update uses PUT with resource ID in URL [OK]
Common Mistakes:
- Using POST with action verbs in URL
- Using GET for updates
- Confusing DELETE with update
