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)