REST API Masterclass
Architecting Modern Backends01.Home02.What is REST?03.HTTP Deep Dive04.URI Best Practices05.JSON & Data Formats06.Environment Setup07.Your First Resource08.Advanced Controllers09.Database Strategy10.JWT Authentication11.Role-Based Auth (RBAC)12.API Versioning13.Filtering & Searching14.Pagination & Sorting15.Global Error Handling16.Rate Limiting17.CORS & Security18.Swagger & OpenAPI19.Testing with Supertest20.Webhooks & Caching21.Production Checklist
Your First Resource (CRUD)
Let's put the principles into practice. We'll design a Productsresource following the industry-standard CRUD (Create, Read, Update, Delete) pattern.
The Standard Endpoint Map
| Method | URI | Result |
|---|---|---|
GET | /products | Find all products |
GET | /products/:id | Find one specific product |
POST | /products | Create a new product |
PUT | /products/:id | Replace a product entirely |
PATCH | /products/:id | Update parts of a product |
DELETE | /products/:id | Remove a product |
Implementation Pattern
A professional route handler should perform 4 steps: Parse, Validate, Execute, and Respond.
// POST /products Example (Express-like pseudo-code)
app.post('/products', (req, res) => {
const { name, price } = req.body; // Parse
if (!name || price < 0) { // Validate
return res.status(400).json({ error: 'Invalid data' });
}
const product = await Product.create({ name, price }); // Execute
res.status(201).json({ status: 'success', data: { product } }); // Respond
});Key Insight: Notice how the response doesn't just send the data. It wraps it in a
data property. This allows you to addMetadata (like pagination or timing) later without breaking clients.