0
0
NestJSframework~10 mins

Status codes and headers in NestJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Status codes and headers
Receive HTTP Request
Controller handles request
Set Status Code
Set Headers
Send Response with Body
Client receives response
The server receives a request, the controller sets status code and headers, then sends the response back to the client.
Execution Sample
NestJS
import { Controller, Get, Res } from '@nestjs/common';

@Controller('example')
export class ExampleController {
  @Get()
  getExample(@Res() res) {
    res.status(201).setHeader('X-Custom-Header', 'NestJS').send('Created');
  }
}
This code sends a response with status 201 and a custom header 'X-Custom-Header'.
Execution Table
StepActionStatus CodeHeaders SetResponse BodyEffect
1Receive GET request at /exampleN/ANoneNoneStart processing request
2Controller method getExample calledN/ANoneNonePrepare response
3Set status code to 201201NoneNoneResponse status set to Created
4Set header 'X-Custom-Header' to 'NestJS'201X-Custom-Header: NestJSNoneCustom header added
5Send response body 'Created'201X-Custom-Header: NestJSCreatedResponse sent to client
6Client receives response201X-Custom-Header: NestJSCreatedRequest complete
💡 Response sent with status 201, header set, and body 'Created'
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
statusCodeundefined201201201201
headers{}{}{'X-Custom-Header': 'NestJS'}{'X-Custom-Header': 'NestJS'}{'X-Custom-Header': 'NestJS'}
responseBodyundefinedundefinedundefined'Created''Created'
Key Moments - 3 Insights
Why do we set the status code before sending the response body?
The status code must be set before sending the response because once the body is sent, the response is finalized and headers or status cannot be changed. See execution_table step 3 and 5.
Can we set multiple headers before sending the response?
Yes, you can set multiple headers before sending the response. Each header is added before the send call. In the example, only one header is set at step 4, but more can be added similarly.
What happens if we try to set headers after sending the response?
Headers cannot be modified after the response is sent. Attempting to do so will have no effect or cause an error. The response is finalized at step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the status code at step 4?
A201
B200
Cundefined
D404
💡 Hint
Check the 'Status Code' column at step 4 in the execution_table.
At which step is the response body sent to the client?
AStep 4
BStep 5
CStep 3
DStep 6
💡 Hint
Look for the step where 'Response Body' changes from undefined to 'Created' in the execution_table.
If we add another header before sending, how would the headers look at step 4?
A{}
B{'Content-Type': 'text/plain'}
C{'X-Custom-Header': 'NestJS', 'Content-Type': 'text/plain'}
DHeaders cannot be changed
💡 Hint
Refer to variable_tracker headers row after step 4 and imagine adding one more header.
Concept Snapshot
In NestJS controllers, use res.status(code) to set HTTP status.
Use res.setHeader(name, value) to add headers.
Call res.send(body) last to send response.
Set status and headers before sending.
This controls how clients understand the response.
Full Transcript
In NestJS, when a controller receives a request, it prepares a response by setting a status code and headers before sending the response body. The status code tells the client if the request was successful or not, like 201 for created. Headers add extra info, like custom metadata. The response body is sent last, finalizing the response. Trying to change status or headers after sending won't work. This flow ensures clients get clear, complete responses.