DEV Community

Cover image for Understanding Backend Routing Like a Pro
Yukti Sahu
Yukti Sahu

Posted on

Understanding Backend Routing Like a Pro

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);
Enter fullscreen mode Exit fullscreen mode

πŸ›£οΈ 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);
}
Enter fullscreen mode Exit fullscreen mode

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);
Enter fullscreen mode Exit fullscreen mode

2️⃣ Dynamic Routes

Dynamic routes accept variables using :.

app.get('/users/:id', getUser);
app.get('/posts/:postId/comments/:commentId', getComment);
Enter fullscreen mode Exit fullscreen mode

From /users/21 β†’ req.params.id is 21.


πŸ” Semantic Routing/Search

Semantic URLs are meaningful and readable.

Example:

GET /products/search/shoes
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

🧩 Nested Routes

Represent relationships like:

User β†’ Posts β†’ Comments

Examples:

/users/:id/posts
/users/:id/posts/:postId/comments
Enter fullscreen mode Exit fullscreen mode

πŸ—‚οΈ Route Versioning

Your API evolves. Older apps still depend on older endpoints.

That's why you version:

/api/v1/users
/api/v2/users
Enter fullscreen mode Exit fullscreen mode

🧹 Route Deprecation

Mark older routes as deprecated before retiring them.

Example JSON:

{ "message": "v1/users is deprecated. Please migrate to v2." }
Enter fullscreen mode Exit fullscreen mode

🌍 Catch-All Routes

A catch-all route handles undefined paths.

app.get('*', (req, res) => {
  res.status(404).json({ message: 'Route not found' });
});
Enter fullscreen mode Exit fullscreen mode

🌟 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)