Controllers in NestJS act like traffic officers. They listen for incoming requests and decide what to do with them.
Why controllers handle incoming requests in NestJS
import { Controller, Get, Post } from '@nestjs/common'; @Controller('cats') export class CatsController { @Get() findAll() { return 'This action returns all cats'; } @Post() create() { return 'This action adds a new cat'; } }
@Controller('route') defines the base route for all methods inside the controller.
Methods inside the controller use decorators like @Get() or @Post() to handle specific HTTP requests.
import { Controller, Get } from '@nestjs/common'; @Controller('dogs') export class DogsController { @Get() getDogs() { return 'List of dogs'; } }
import { Controller, Post } from '@nestjs/common'; @Controller('birds') export class BirdsController { @Post() addBird() { return 'New bird added'; } }
This controller manages a list of fruits. It returns all fruits on GET requests and adds an orange on POST requests.
import { Controller, Get, Post } from '@nestjs/common'; @Controller('fruits') export class FruitsController { private fruits = ['apple', 'banana']; @Get() getAllFruits() { return this.fruits; } @Post() addFruit() { this.fruits.push('orange'); return this.fruits; } }
Controllers only handle routing and request reception. Business logic should be in services.
Use decorators to clearly define which HTTP method each controller method handles.
Controllers listen for and handle incoming requests in NestJS.
They organize routes and connect requests to the right code.
Keep controllers focused on routing; use services for data and logic.
Practice
Solution
Step 1: Understand the role of controllers in NestJS
Controllers are designed to listen for incoming HTTP requests and decide which code should handle them.Step 2: Differentiate responsibilities
Controllers focus on routing and connecting requests to services, not on data storage or UI rendering.Final Answer:
Because they organize routes and connect requests to the right code -> Option DQuick Check:
Controllers handle routing = A [OK]
- Thinking controllers manage data storage
- Confusing controllers with UI components
- Assuming controllers handle hardware
Solution
Step 1: Recognize NestJS controller method syntax
In NestJS, @Get() decorator marks a method to handle GET requests inside a controller class.Step 2: Compare options
@Get() async handleRequest() { return 'Hello'; } uses @Get() decorator correctly; others use incorrect syntax or patterns from other frameworks.Final Answer:
@Get() async handleRequest() { return 'Hello'; } -> Option CQuick Check:
@Get() decorator = C [OK]
- Using plain functions outside controller classes
- Confusing @Controller with method decorators
- Using Express router syntax inside NestJS controllers
@Controller('greet')
export class GreetController {
@Get()
sayHello() {
return 'Hello, NestJS!';
}
}Solution
Step 1: Analyze the route path
The controller is set to 'greet' and the method handles GET requests at the root of this path, so the full route is '/greet'.Step 2: Check the method return value
The sayHello() method returns the string 'Hello, NestJS!'. This string will be sent as the response body.Final Answer:
'Hello, NestJS!' -> Option BQuick Check:
GET /greet returns method string = A [OK]
- Expecting 404 due to missing route
- Thinking method returns nothing
- Assuming syntax errors without checking code
@Controller('users')
export class UserController {
@Post('/create')
createUser() {
// missing request body parameter
return 'User created';
}
}Solution
Step 1: Review method parameters for POST requests
POST requests usually send data in the request body, which the method should accept as a parameter decorated with @Body().Step 2: Check the method signature
The createUser() method has no parameters, so it cannot access the incoming data, which is a common mistake.Final Answer:
The method lacks a parameter to receive the request body -> Option AQuick Check:
POST methods need @Body() parameter = B [OK]
- Ignoring request body in POST methods
- Misusing decorators on methods
- Thinking controller path causes error
Solution
Step 1: Understand separation of concerns in NestJS
Controllers should only handle routing and delegate business logic to services to keep code clean and maintainable.Step 2: Identify the correct pattern
Injecting a service into the controller and calling its methods inside controller handlers follows this best practice.Final Answer:
Inject a service into the controller and call its methods inside controller handlers -> Option AQuick Check:
Controllers route, services handle logic = D [OK]
- Putting business logic inside controllers
- Accessing database directly in controllers
- Mixing routing and logic in one place
Ready to prove it?
Pass the test and this pattern moves unknown
