What Is a NestJS Controller? Routing HTTP Requests to Providers
A NestJS controller is the boundary that receives HTTP requests and sends responses to clients. Decorators such as `@Controller()`, `@Get()`, and `@Post()` connect URL paths and methods to class methods. Keep request and response handling at this boundary, and delegate data access or complex application work to a provider such as a service so each responsibility is easier to understand and review.
A NestJS controller is the boundary that receives HTTP requests and sends responses to clients. Decorators such as @Controller(), @Get(), and @Post() connect URL paths and methods to class methods. Keep request and response handling at this boundary, and delegate data access or complex application work to a provider such as a service so each responsibility is easier to understand and review.
What does a controller do in the request flow?
When a request arrives, NestJS routing selects a controller and handler. A controller handles a particular set of requests and can expose several paths and actions. The class decorator @Controller('cats') defines a shared path prefix, while a method decorator such as @Get() or @Post() defines the HTTP method and optional path below it. (NestJS: Controllers)
For example, @Get(':id') inside @Controller('cats') handles a request to GET /cats/:id. Decorators are not merely comments: NestJS uses their metadata to build the routing map. To find the full route, combine the controller prefix with the method path. (NestJS: Controllers)
How should controller and service responsibilities be divided?
A controller should receive the needed request values, call the appropriate application operation, and return its result as an HTTP response. Data storage and retrieval or more complex application logic can live in a provider, commonly a service. NestJS documentation describes controllers as handling requests and delegating more complex tasks to providers. (NestJS: Providers)
This boundary is a design approach, not a rule that the framework enforces on every line of code. If storage access, policy decisions, and input conversion all accumulate in a controller, handlers may repeat rules and the work can be difficult to reuse outside HTTP. At the other extreme, moving even a simple HTTP input/output mapping into a service can add an unnecessary layer.
How do handlers receive paths, parameters, and request bodies?
NestJS parameter decorators such as @Param(), @Query(), and @Body() bind path parameters, query values, and request bodies to handler arguments. These values come from outside the application. A TypeScript type annotation alone does not mean the runtime value has been validated. Configure a pipe such as ValidationPipe to validate a DTO or schema; when converting a string ID to a number, also handle malformed values and allowed ranges. (NestJS: Controllers, NestJS: Validation)
How do you connect a controller to a service?
The following example shows a minimal setup in which CatsController receives a path parameter and delegates lookup to CatsService. The service must be registered as a module provider for NestJS dependency injection to provide it to the controller.
import { Controller, Get, Injectable, Module, Param } from '@nestjs/common';
@Injectable()
class CatsService {
findOne(id: string) {
return { id, name: 'Milo' };
}
}
@Controller('cats')
class CatsController {
constructor(private readonly catsService: CatsService) {}
@Get(':id')
findOne(@Param('id') id: string) {
return this.catsService.findOne(id);
}
}
@Module({
controllers: [CatsController],
providers: [CatsService],
})
class CatsModule {}This code illustrates NestJS decorators and provider wiring; it has not been run or verified. Registering the controller and service in a NestJS module defines the route as GET /cats/:id. findOne() returns an example name kept in memory; it does not query a database. Depending on project setup, TypeScript decorator compiler options and other standard configuration may be needed. (NestJS: Controllers, NestJS: Providers)
What should you check when a controller grows or does not work?
Combine the `@Controller()` prefix and method decorator path to determine the actual request URL.
Confirm that the request uses the HTTP method declared by the handler decorator.
Check where path parameters, query values, and bodies come from, and validate external input with a pipe or validation layer.
If storage access or complex policy decisions accumulate in the controller, identify which responsibilities belong in a provider.
If dependency injection fails, confirm that the service is listed under the module's `providers`.
Do not assume class properties and return types automatically validate runtime input or guarantee response serialization; check the pipes, interceptors, and framework configuration actually applied.