The Journey of Data — "Frontend → Backend → Database → Frontend"
That’s an excellent question — and it’s one that every good backend/frontend developer must deeply understand. 💡
Let’s make this super clear and visual 👇
🧩 1. The Journey of Data — "Frontend → Backend → Database → Frontend"
When you click a button or send a form, data travels from frontend → backend → database (and sometimes back).
But how the data travels depends on how you send it — via:
-
URL (params or query)
-
Request body
-
Headers
-
Cookies
Let’s break these down one by one.
⚙️ 2. req.params — URL Parameters (Part of the Route)
Example:
Frontend:
await axios.get('/api/user/123');
Backend:
app.get('/api/user/:id', (req, res) => {
console.log(req.params.id); // "123"
});
🧠 Use req.params when:
-
The data identifies a specific resource.
Example:/user/:id,/post/:postId/comments,/delete/:id -
It’s part of the URL path, not optional.
✅ Example uses:
-
Get a specific user:
/users/:id -
Delete a specific post:
/posts/:id
📦 3. req.body — Request Body (Usually for POST/PUT)
Example:
Frontend:
await axios.post('/api/register', {
name: 'D K Sinha',
email: 'dk@example.com',
password: '123456',
});
Backend:
app.post('/api/register', (req, res) => {
console.log(req.body.name); // "D K Sinha"
});
🧠 Use req.body when:
-
Sending data or payloads (forms, JSON, uploads)
-
Creating, updating, or deleting records
✅ Example uses:
-
Register user
-
Login with credentials
-
Submit message or image
🔍 4. req.query — Query Parameters (After ? in URL)
Example:
Frontend:
await axios.get('/api/search?keyword=apple&page=2');
Backend:
app.get('/api/search', (req, res) => {
console.log(req.query.keyword); // "apple"
console.log(req.query.page); // "2"
});
🧠 Use req.query when:
-
You’re filtering, sorting, or searching data
-
Parameters are optional
-
URL looks like
/api/search?term=abc&limit=10
✅ Example uses:
-
Pagination, filters, search
-
Sorting results
🍪 5. req.cookies — Cookies (Sent Automatically)
Example:
Frontend:
const axiosInstance = axios.create({
baseURL: 'http://localhost:4000',
withCredentials: true,
});
await axiosInstance.get('/api/profile');
Backend:
app.get('/api/profile', (req, res) => {
console.log(req.cookies.jwt); // token string
});
🧠 Use req.cookies when:
-
You’re managing authentication (JWT, sessions)
-
You want automatic credentials sent with every request
✅ Example uses:
-
Check logged-in user
-
Auto-login with token
🔐 6. req.headers — Metadata or Tokens
Example:
Frontend:
await axios.get('/api/data', {
headers: {
Authorization: 'Bearer your_jwt_token',
},
});
Backend:
app.get('/api/data', (req, res) => {
console.log(req.headers.authorization); // "Bearer your_jwt_token"
});
🧠 Use req.headers when:
-
Sending access tokens
-
Sending API keys
-
Custom client info (rare)
✅ Example uses:
-
Authorization token
-
Content type
-
User agent info
🧠 7. Summary Table (When to Use What)
| Purpose | Example | Frontend Sends | Backend Reads From |
|---|---|---|---|
| Get specific item | /api/user/123 |
In URL | req.params |
| Filter or search | /api/search?term=apple |
After ? |
req.query |
| Create or update | { name: "DK", email: "x" } |
In body | req.body |
| Auth token cookie | Sent automatically | Browser cookie | req.cookies |
| Auth token header | "Bearer token" |
Header | req.headers |
🧩 8. Frontend to Backend — How You Decide What to Use
Think of it like this:
| Goal | What you use |
|---|---|
| Fetch/delete a specific resource | params |
| Submit form data or object | body |
| Search/filter | query |
| Auth with cookie | cookie |
| Auth with token | header |
🧭 9. How to Know Which One You Used
You can inspect requests in your browser or backend logs:
🔹 In Browser:
-
Open Developer Tools → Network tab
-
Click your request
-
See:
-
URL → params or query
-
Request Payload → body
-
Cookies / Headers → credentials
-
🔹 In Backend:
Add quick debug:
console.log('params:', req.params);
console.log('body:', req.body);
console.log('query:', req.query);
console.log('cookies:', req.cookies);
That way you instantly see what’s arriving.
🔥 Quick Rule of Thumb (Easy to Remember)
| Type | When to Use | Mnemonic |
|---|---|---|
req.params |
Resource path | Permanent path |
req.query |
Optional filters | Question mark = Query |
req.body |
Sending data | Body = Bulk data |
req.cookies |
Login session | Cookies = Credentials |
req.headers |
Token/Auth | Headers = Hidden info |
Would you like me to give you a visual flow diagram showing how data travels between frontend and backend with all five (params, query, body, cookie, header)? It helps lock this in permanently.
Comments