Why Controllers Handle Incoming Requests in NestJS
📖 Scenario: You are building a simple web server using NestJS. You want to organize your code so that it can respond to users' requests properly.
🎯 Goal: Learn how to create a controller in NestJS that handles incoming HTTP GET requests and sends back a response.
📋 What You'll Learn
Create a controller class named AppController
Add a method named getHello inside AppController
Use the @Controller() decorator on the class
Use the @Get() decorator on the getHello method
Return the string 'Hello World!' from getHello
💡 Why This Matters
🌍 Real World
Web servers use controllers to respond to user requests like loading pages or submitting forms.
💼 Career
Understanding controllers is essential for backend developers working with NestJS or similar frameworks to build APIs and web services.
Progress0 / 4 steps
1
Create the Controller Class
Create a class called AppController and decorate it with @Controller().
NestJS
Hint
The @Controller() decorator marks the class as a controller that can handle requests.
2
Add a Method to Handle GET Requests
Inside AppController, add a method named getHello and decorate it with @Get().
NestJS
Hint
The @Get() decorator tells NestJS this method handles GET requests.
3
Return a Response String
Make the getHello method return the string 'Hello World!'.
NestJS
Hint
Returning a string sends it as the HTTP response body.
4
Complete the Controller Setup
Ensure the AppController class is exported and properly decorated with @Controller() and the getHello method is decorated with @Get() returning 'Hello World!'.
NestJS
Hint
This is the complete controller that handles incoming GET requests and returns a response.
Practice
(1/5)
1. In NestJS, why do controllers handle incoming requests?
easy
A. Because they manage server hardware resources
B. Because they store data and manage databases
C. Because they handle user interface rendering
D. Because they organize routes and connect requests to the right code
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 D
Quick Check:
Controllers handle routing = A [OK]
Hint: Controllers route requests to code, not store data [OK]
Common Mistakes:
Thinking controllers manage data storage
Confusing controllers with UI components
Assuming controllers handle hardware
2. Which of the following is the correct way to define a controller method to handle GET requests in NestJS?
easy
A. @Controller() get() { return 'Hello'; }
B. function get() { return 'Hello'; }
C. @Get() async handleRequest() { return 'Hello'; }
D. router.get('/hello', () => 'Hello');
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 C
Quick Check:
@Get() decorator = C [OK]
Hint: Use @Get() decorator for GET routes in controllers [OK]
Common Mistakes:
Using plain functions outside controller classes
Confusing @Controller with method decorators
Using Express router syntax inside NestJS controllers
3. Given this NestJS controller code, what will be the output when a GET request is made to '/greet'?
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 B
Quick Check:
GET /greet returns method string = A [OK]
Hint: Controller path + method path = full route [OK]
Common Mistakes:
Expecting 404 due to missing route
Thinking method returns nothing
Assuming syntax errors without checking code
4. Identify the error in this NestJS controller code snippet:
@Controller('users')
export class UserController {
@Post('/create')
createUser() {
// missing request body parameter
return 'User created';
}
}
medium
A. The method lacks a parameter to receive the request body
B. The @Post decorator is used incorrectly
C. The controller path should not be 'users'
D. The method should return a Promise, not a string
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 A
Quick Check:
POST methods need @Body() parameter = B [OK]
Hint: POST handlers need @Body() to get data [OK]
Common Mistakes:
Ignoring request body in POST methods
Misusing decorators on methods
Thinking controller path causes error
5. You want to keep your NestJS controller focused only on routing and delegate business logic to a service. Which approach best follows this principle?
hard
A. Inject a service into the controller and call its methods inside controller handlers
B. Handle HTTP requests and business logic in the same controller method
C. Use controllers to directly access the database
D. Write all data processing inside the controller methods
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 A
Quick Check:
Controllers route, services handle logic = D [OK]
Hint: Use services for logic, controllers for routing [OK]
Common Mistakes:
Putting business logic inside controllers
Accessing database directly in controllers
Mixing routing and logic in one place
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.