This module is extremely important for full-stack and frontend engineers.
Companies donβt just check if you can call APIs.
They check if you understand architecture, scalability, and clean structure.
Letβs go step by step.
β 4.1 REST Principles
REST = Representational State Transfer
It is an architectural style for designing APIs.
πΉ 1οΈβ£ Stateless
Every request must contain all information required to process it.
Server does NOT remember previous request.
Example:
GET /users/123
Authorization: Bearer token
If token missing β request fails.
Server does not βrememberβ user automatically.
Why Stateless is Important?
- Easier to scale
- Load balancers work better
- No session memory dependency
πΉ 2οΈβ£ Resource-Based
In REST, everything is a resource.
Examples:
/users
/users/1
/orders
/products/10
NOT:
/getUser
/createProduct
/deleteOrder
Use nouns, not verbs.
πΉ 3οΈβ£ HTTP Methods
Actions are defined by HTTP methods, not URL.
Example:
GET /users
POST /users
PUT /users/1
DELETE /users/1
Clean. Predictable. Standardized.
β 4.2 HTTP Methods (Deep Understanding)
πΉ GET
Used to retrieve data.
- No body
- Should not modify data
- Cacheable
Example:
GET /products
πΉ POST
Used to create new resource.
- Has request body
- Not idempotent
Example:
POST /users
πΉ PUT
Used to fully update resource.
Replaces entire resource.
Example:
PUT /users/1
If field missing β overwritten.
πΉ PATCH
Used to partially update resource.
Example:
PATCH /users/1
{
"name": "Nadim"
}
Only updates name.
πΉ DELETE
Deletes resource.
DELETE /users/1
π₯ Interview Question
Difference between PUT and PATCH?
PUT β Full replacement
PATCH β Partial update
β 4.3 Important Status Codes
These are extremely common in interviews.
πΉ 200 OK
Request successful.
Used for:
- GET success
- Update success
πΉ 201 Created
Resource successfully created.
Used after POST.
πΉ 400 Bad Request
Client sent invalid data.
Example:
- Missing required field
- Invalid JSON
πΉ 401 Unauthorized
User not authenticated.
- Missing token
- Invalid token
πΉ 403 Forbidden
User authenticated but not allowed.
Example:
User role = user
Trying to access admin route.
πΉ 404 Not Found
Resource doesnβt exist.
πΉ 500 Internal Server Error
Server crashed or unexpected error.
Never expose sensitive details here.
β 4.4 API Integration (Frontend Side)
Now we move to practical frontend architecture.
πΉ Axios Interceptors
Interceptors allow you to intercept requests or responses globally.
Add Token Automatically
axios.interceptors.request.use(config => {
const token = localStorage.getItem("token");
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
});
Global Response Handling
axios.interceptors.response.use(
response => response,
error => {
if (error.response.status === 401) {
console.log("Unauthorized");
}
return Promise.reject(error);
}
);
πΉ Global Error Handling
Instead of handling errors everywhere:
Create one centralized handler.
Example:
function handleApiError(error) {
if (error.response) {
console.log(error.response.data.message);
} else {
console.log("Network error");
}
}
Keeps UI clean.
πΉ Retry Mechanism
If request fails due to network issue:
Retry automatically.
Basic example:
async function fetchWithRetry(fn, retries = 3) {
try {
return await fn();
} catch (err) {
if (retries === 0) throw err;
return fetchWithRetry(fn, retries - 1);
}
}
Useful for:
- Temporary network failures
- Microservice communication
πΉ Refresh Token Logic (Frontend Flow)
Typical flow:
- Access token expires
- API returns 401
- Interceptor detects 401
- Call
/refreshendpoint - Get new access token
- Retry original request
Example Pattern:
axios.interceptors.response.use(
res => res,
async error => {
if (error.response.status === 401) {
await refreshToken();
return axios(error.config);
}
return Promise.reject(error);
}
);
Important:
Avoid infinite loops.
β 4.5 Frontend Architecture
This is where junior and senior developers differ.
πΉ Feature-Based Folder Structure
Instead of:
components/
pages/
utils/
services/
Use:
features/
auth/
components/
services/
hooks/
products/
components/
services/
Benefits:
- Scalable
- Easy to maintain
- Clear ownership
πΉ Service Layer Abstraction
Never call API directly inside component.
β Bad:
useEffect(() => {
axios.get("/users");
}, []);
β
Good:
// services/userService.js
export const getUsers = () => api.get("/users");
Then use inside component.
Benefits:
- Reusable
- Testable
- Clean separation
πΉ Clean Code Principles
- Meaningful variable names
- Small functions
- Avoid duplication
- Single responsibility
- Clear error handling
πΉ Separation of Concerns
UI should NOT handle:
- Business logic
- API details
- Data transformation
Each layer should have one responsibility:
- UI Layer β Display
- Service Layer β API communication
- Business Logic Layer β Processing
- Utility Layer β Helpers
π§ Interview-Level Thinking
They may ask:
β How would you structure a scalable frontend app?
Strong answer:
- Feature-based structure
- Centralized API layer
- Axios interceptors
- Token refresh logic
- Global error handling
- Reusable hooks
- Clean separation of concerns
π― Final Understanding
If you master:
- REST principles
- Proper HTTP usage
- Status codes
- Secure API integration
- Token refresh flow
- Clean architecture
You are not just coding.
You are building production-ready systems.
Top comments (0)