0
0
NestJSframework~10 mins

Response handling in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Response handling
Receive HTTP Request
Controller Method Called
Process Request Logic
Prepare Response Data
Send Response to Client
Client Receives Response
This flow shows how NestJS handles an HTTP request by calling a controller, processing logic, preparing data, and sending the response back to the client.
Execution Sample
NestJS
import { Controller, Get, Res } from '@nestjs/common';
import { Response } from 'express';

@Controller('hello')
export class HelloController {
  @Get()
  getHello(@Res() res: Response) {
    res.status(200).json({ message: 'Hello World' });
  }
}
This code defines a NestJS controller that sends a JSON response with status 200 and a message when the /hello route is requested.
Execution Table
StepActionInput/StateOutput/State ChangeNotes
1Receive HTTP GET /helloRequest receivedController method getHello calledNestJS routes request to controller
2Call getHello methodNo parameters except response objectPrepare response with status 200 and JSON bodyUses Express Response object
3Execute res.status(200).json(...)Response objectHTTP response sent with status 200 and JSON { message: 'Hello World' }Response sent to client
4Client receives responseHTTP 200 with JSON bodyClient can read message 'Hello World'End of request-response cycle
💡 Response sent to client, request handling complete
Variable Tracker
VariableStartAfter Step 2After Step 3Final
resExpress Response objectResponse status set to 200Response body set to JSON { message: 'Hello World' }Response sent to client
Key Moments - 2 Insights
Why do we use @Res() to get the response object instead of just returning data?
Using @Res() gives full control over the response, like setting status codes and headers manually, as shown in step 2 and 3 of the execution_table.
What happens if we forget to call res.status(...).json(...) inside the controller?
The client will not receive a response and the request will hang, because the response is never sent. This is clear from step 3 where sending the response ends the cycle.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the response status code set at step 3?
A500
B404
C200
D302
💡 Hint
Check the 'Output/State Change' column at step 3 in the execution_table.
At which step is the controller method getHello called?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Action' column to find when getHello is executed.
If we remove @Res() and just return { message: 'Hello World' }, what changes in the response handling?
ANestJS automatically sends JSON with status 200
BResponse status must be set manually
CResponse will not be sent
DClient receives plain text instead of JSON
💡 Hint
Consider how NestJS handles return values when @Res() is not used.
Concept Snapshot
NestJS Response Handling:
- Use @Res() to access Express response object
- Call res.status(code).json(data) to send JSON with status
- Without @Res(), return value is auto-sent as JSON with status 200
- Response ends request cycle
- Control response headers and status manually with @Res()
Full Transcript
In NestJS, when a client sends an HTTP request, the framework routes it to the matching controller method. Inside the method, you can use the @Res() decorator to get the Express response object. This lets you set the HTTP status code and send JSON data manually using res.status(200).json({ message: 'Hello World' }). This sends the response back to the client and ends the request cycle. If you do not use @Res(), you can simply return an object and NestJS will automatically send it as JSON with status 200. The execution table shows each step from receiving the request, calling the controller, preparing the response, and sending it. The variable tracker shows how the response object changes as status and body are set. Key moments clarify why @Res() is used and what happens if the response is not sent. The quiz tests understanding of status codes, method calls, and automatic response sending.