Routing is the part of a backend that maps an incoming request, defined by its HTTP method and URL path, to the code that should handle it. A controller is that code: a function or method that reads the request, calls the business logic, and builds the response. Together, backend routing and controllers keep a web application organised and predictable.
In this guide you will see how routes are declared, what path and query parameters are, how controllers differ from services, and how the request travels from the router to the response.
Every HTTP request has a method (GET, POST, PUT, PATCH, DELETE) and a path. The router holds a table of rules and picks the first one that matches. Each rule points to a handler.
| Request | Meaning | Handler |
|---|---|---|
GET /products | List products | listProducts |
GET /products/42 | Read one product | getProduct |
POST /products | Create a product | createProduct |
PATCH /products/42 | Update part of a product | updateProduct |
DELETE /products/42 | Remove a product | deleteProduct |
This naming style, where the path names a resource (a noun) and the method names the action, is the basis of REST-style APIs. If no route matches, the router should return 404. If the path matches but the method does not, it should return 405 with an Allow header.
Here is routing in Express 5, which most Node.js developers meet first:
There are three places data can arrive:
:id in /products/42, identify one resource. They are read from req.params.?page=2&sort=price, filter or page a collection. They are read from req.query.req.body after a parser middleware runs.Other frameworks look the same in spirit. Django uses path("products/<int:pk>/", view), Flask uses @app.get("/products/<int:pk>"), and Spring uses @GetMapping("/products/{id}").
A controller is the thin layer between HTTP and your application logic. Its job is to translate:
A controller should not contain SQL, pricing rules or email sending. Those belong in a service or domain layer, which can be tested without HTTP and reused by other entry points such as a queue worker or a command-line tool.
The router is mounted at /products, so the route /:id becomes /products/:id. Grouping routes by resource like this keeps each file small.
Routers test rules in order, so specific paths must come before general ones. If /products/:id is declared before /products/featured, a request for featured is treated as an id. Register /products/featured first.
Common organisation habits:
/api/v1 so you can change the contract without breaking existing clients.| Situation | Status |
|---|---|
| Successful read or update | 200 |
| Resource created | 201 |
| Success with nothing to return | 204 |
| Invalid input | 400 or 422 |
| Not signed in | 401 |
| Signed in but not allowed | 403 |
| Not found | 404 |
| Unexpected server error | 500 |
Returning the right code lets clients, caches and monitoring tools behave correctly. A 200 response that contains an error message hides failures.
A route is the rule that matches a method and path. A controller is the function that runs when the rule matches and produces the response.
In a service or domain layer, not in the controller. The controller handles HTTP details and calls the service.
Path parameters identify a specific resource (/users/7). Query parameters adjust a request, such as filtering, sorting or paging (/users?role=admin).
Check the HTTP method, the mount prefix, a trailing slash, and the order of routes. A broader route earlier in the file may also be catching the request.
Routing connects a method and path to a handler, and controllers are the thin handlers that translate between HTTP and your application logic. Read input from path, query and body, delegate to services, return meaningful status codes, and organise routers per resource with a version prefix.