REST design principles help make web services simple, clear, and easy to use. They guide how to organize and communicate data over the internet.
Why REST design principles matter in Node.js
GET /items - Retrieve a list of items
POST /items - Create a new item
GET /items/{id} - Retrieve a specific item
PUT /items/{id} - Update a specific item
DELETE /items/{id} - Delete a specific itemUse HTTP methods (GET, POST, PUT, DELETE) to represent actions.
Use clear and consistent URLs to represent resources.
GET /books POST /books GET /books/123 PUT /books/123 DELETE /books/123
GET /users POST /users GET /users/45 PUT /users/45 DELETE /users/45
This Node.js example uses Express to create a simple REST API for managing items. It shows how to use REST methods to get, add, update, and delete items.
import express from 'express'; const app = express(); app.use(express.json()); const items = [{ id: 1, name: 'Apple' }, { id: 2, name: 'Banana' }]; app.get('/items', (req, res) => { res.json(items); }); app.post('/items', (req, res) => { const newItem = { id: items.length + 1, name: req.body.name }; items.push(newItem); res.status(201).json(newItem); }); app.get('/items/:id', (req, res) => { const item = items.find(i => i.id === Number(req.params.id)); if (item) { res.json(item); } else { res.status(404).send('Item not found'); } }); app.put('/items/:id', (req, res) => { const item = items.find(i => i.id === Number(req.params.id)); if (item) { item.name = req.body.name; res.json(item); } else { res.status(404).send('Item not found'); } }); app.delete('/items/:id', (req, res) => { const index = items.findIndex(i => i.id === Number(req.params.id)); if (index !== -1) { items.splice(index, 1); res.status(204).send(); } else { res.status(404).send('Item not found'); } }); app.listen(3000, () => { console.log('Server running on http://localhost:3000'); });
Following REST principles makes your API predictable and easier to use.
Use proper HTTP status codes to communicate success or errors clearly.
Keep URLs simple and resource-focused for better understanding.
REST principles help organize web services clearly and simply.
They use HTTP methods and URLs to represent actions and resources.
Following these principles makes APIs easier to use and maintain.
Practice
Solution
Step 1: Understand REST principles purpose
REST principles guide API design to be clear and predictable by using standard HTTP methods and resource URLs.Step 2: Evaluate options against REST goals
They make APIs easier to understand and use by following clear rules. matches the goal of making APIs easier to understand and use. The other options contradict REST principles.Final Answer:
They make APIs easier to understand and use by following clear rules. -> Option AQuick Check:
REST clarity = A [OK]
- Thinking REST requires only one HTTP method
- Confusing REST with data format rules
- Ignoring URL structure importance
Solution
Step 1: Recall REST HTTP method meanings
GET retrieves data, POST creates, PUT updates, DELETE removes resources.Step 2: Match methods to actions
PUTto update an existing resource correctly pairs PUT with updating a resource. Other options mismatch methods and actions.Final Answer:
PUT to update an existing resource -> Option BQuick Check:
PUT = update [OK]
- Mixing POST and GET actions
- Using DELETE to fetch data
- Confusing PUT with POST
app.delete('/users/:id', (req, res) => {
// delete user logic
res.send('User deleted');
});What is the expected behavior when a client sends a DELETE request to
/users/123?Solution
Step 1: Analyze the HTTP method and route
The route uses DELETE method on '/users/:id', indicating deletion of a user by ID.Step 2: Understand the response
The handler sends 'User deleted' after deletion logic, confirming the user is deleted.Final Answer:
It deletes user 123 and responds with confirmation. -> Option CQuick Check:
DELETE /users/:id = delete user [OK]
- Confusing DELETE with GET or POST
- Expecting data retrieval on DELETE
- Ignoring route parameter usage
app.post('/items/:id', (req, res) => {
// update item logic
res.send('Item updated');
});What is the main REST design issue with this code?
Solution
Step 1: Identify HTTP method and action mismatch
POST is typically used to create new resources, not update existing ones.Step 2: Apply REST principle for updating
PUT is the correct method to update a resource identified by an ID in the URL.Final Answer:
POST should not be used to update an existing resource; PUT is preferred. -> Option AQuick Check:
Update = PUT, not POST [OK]
- Using POST for updates
- Ignoring URL parameter meaning
- Confusing response messages with method purpose
Solution
Step 1: Identify RESTful update method
PUT is the standard HTTP method to update an existing resource fully or partially.Step 2: Check URL structure for resource identification
/books/42 correctly identifies the book resource by ID, matching REST URL design.Step 3: Evaluate other options
POST is for creation, GET should not change data, DELETE removes resource, so they are incorrect here.Final Answer:
PUT /books/42 with new title in body -> Option DQuick Check:
Update book = PUT /books/42 [OK]
- Using POST to update existing resource
- Using GET to change data
- Misusing DELETE for updates
Ready to prove it?
Pass the test and this pattern moves unknown
