When you send a request to your server, two things matter:
- WHAT you want to do → defined by HTTP methods
- WHERE you want to do it → handled by Routing
Together, they decide what action to perform and on which resource.
And then your handler function steps in to run the business logic.
🌐 HTTP Methods = "What do you want to do?"
HTTP methods describe the intention of your request.
Method Meaning Example
GET "Give me data" /users → fetch users
POST "Create something" /users → add a new user
PUT "Replace this fully" /users/10
PATCH "Update partially" /users/10
DELETE "Remove this" /users/10
Example (Express.js)
app.get('/users', getAllUsers);
app.post('/users', createUser);
app.delete('/users/:id', deleteUser);
🛣️ Routing = "Where do you want to go?"
Routes tell the server:
- which endpoint the request targets\
- which resource to operate on\
- which handler will run your logic
Example:
/users → returns an array of all users\
/users/5 → returns data of user with ID 5
Routing literally maps URLs → functions.
🧠 Handlers = Your Business Logic
function getAllUsers(req, res) {
res.json(users);
}
Routing = address\
Handler = the function cooking the response
🔥 Static vs Dynamic Routing
1️⃣ Static Routes
These routes are fixed.
app.get('/about', aboutPage);
app.get('/contact', contactPage);
2️⃣ Dynamic Routes
Dynamic routes accept variables using :.
app.get('/users/:id', getUser);
app.get('/posts/:postId/comments/:commentId', getComment);
From /users/21 → req.params.id is 21.
🔍 Semantic Routing/Search
Semantic URLs are meaningful and readable.
Example:
GET /products/search/shoes
They are:
- SEO-friendly\
- easy to read\
- more intuitive
❓ Query Parameters
Query params come after ?.
Examples:
/users?limit=10&page=2
/users?active=true
/users/search?name=yukti&sort=asc
🧩 Nested Routes
Represent relationships like:
User → Posts → Comments
Examples:
/users/:id/posts
/users/:id/posts/:postId/comments
🗂️ Route Versioning
Your API evolves. Older apps still depend on older endpoints.
That's why you version:
/api/v1/users
/api/v2/users
🧹 Route Deprecation
Mark older routes as deprecated before retiring them.
Example JSON:
{ "message": "v1/users is deprecated. Please migrate to v2." }
🌍 Catch-All Routes
A catch-all route handles undefined paths.
app.get('*', (req, res) => {
res.status(404).json({ message: 'Route not found' });
});
🌟 Best Practices
- Group routes by feature\
- Use nouns, not verbs\
- Keep routes lowercase\
- Use meaningful names\
- Version your API\
- Add structured error responses
✨ Summary
- HTTP methods = what\
- Routes = where\
- Handlers = logic\
- Static & dynamic routes\
- Query params, nested routes\
- Versioning & deprecation\
- Catch-all routes
Top comments (0)