What is the purpose of response handling in NestJS?
Response handling in NestJS manages how the server sends data back to the client after processing a request. It ensures the client receives the right data, status codes, and headers.
Click to reveal answer
beginner
How do you send a JSON response in a NestJS controller?
You return a JavaScript object or array from the controller method. NestJS automatically converts it to JSON and sends it with a 200 status code.
Click to reveal answer
intermediate
What decorator can you use to set a custom HTTP status code in NestJS?
You use the @HttpCode() decorator above a controller method to set a custom HTTP status code for the response.
Click to reveal answer
intermediate
How can you send a file as a response in NestJS?
You can inject the Response object from Express using @Res() and use its methods like res.sendFile() to send files directly.
Click to reveal answer
advanced
What is the difference between returning data and using @Res() in NestJS controllers?
Returning data lets NestJS handle the response automatically (recommended). Using @Res() gives you full control but disables automatic response handling, so you must manage headers and status codes yourself.
Click to reveal answer
In NestJS, what happens if you return an object from a controller method?
ANestJS ignores the return value.
BNestJS sends it as plain text.
CNestJS throws an error.
DNestJS sends it as a JSON response with status 200.
✗ Incorrect
Returning an object from a controller method automatically sends it as JSON with HTTP status 200.
Which decorator sets a custom HTTP status code for a response in NestJS?
A@ResponseStatus()
B@StatusCode()
C@HttpCode()
D@SetStatus()
✗ Incorrect
The @HttpCode() decorator sets a custom HTTP status code for the response.
What must you do if you use @Res() in a NestJS controller method?
AReturn the response object.
BManually send the response using res methods.
CNothing, NestJS handles the response automatically.
DUse @HttpCode() to set status.
✗ Incorrect
Using @Res() disables automatic response handling, so you must manually send the response.
How do you send a file in a NestJS controller?
AUse @Res() and call res.sendFile().
BReturn the file path as a string.
CUse @HttpCode(201).
DReturn a Buffer object.
✗ Incorrect
Inject the response object with @Res() and use res.sendFile() to send files.
What is the recommended way to handle responses in NestJS?
AReturn data and let NestJS handle the response automatically.
BUse raw Node.js response objects.
CUse console.log to send responses.
DAlways use @Res() for full control.
✗ Incorrect
Returning data lets NestJS handle serialization and status codes automatically, which is simpler and less error-prone.
Explain how NestJS handles response data when you return an object from a controller method.
Think about what NestJS does behind the scenes with returned data.
You got /3 concepts.
Describe the differences and trade-offs between returning data and using the @Res() decorator in NestJS controllers.
Consider ease of use versus control.
You got /3 concepts.
Practice
(1/5)
1. In NestJS, what is the primary purpose of using the @Res() decorator in a controller method?
easy
A. To define a route path for the controller method
B. To inject a service into the controller
C. To access and customize the HTTP response object directly
D. To automatically serialize the response data
Solution
Step 1: Understand the role of @Res()
The @Res() decorator gives direct access to the HTTP response object, allowing full control over the response.
Step 2: Differentiate from other decorators
Other decorators like @Inject() inject services, and route decorators define paths, but @Res() is specifically for response handling.
Final Answer:
To access and customize the HTTP response object directly -> Option C
Quick Check:
@Res() = direct response control [OK]
Hint: Remember: @Res() means direct response access [OK]
Common Mistakes:
Confusing @Res() with service injection
Thinking @Res() defines routes
Assuming @Res() auto-serializes data
2. Which of the following is the correct way to set a 201 status code and send a JSON response using @Res() in NestJS?
easy
A. res.status(201).json({ message: 'Created' });
B. res.sendStatus(201).json({ message: 'Created' });
C. res.json(201, { message: 'Created' });
D. res.statusCode(201).send({ message: 'Created' });
Solution
Step 1: Recall Express response methods
In NestJS, @Res() uses Express response methods. The correct way to set status and send JSON is res.status(201).json(...).
Step 2: Identify incorrect method usage
sendStatus sends status and ends response, so chaining .json() is invalid. res.json(201, ...) and res.statusCode(201) are not valid Express methods.
Final Answer:
res.status(201).json({ message: 'Created' }); -> Option A
Quick Check:
Set status then json = res.status(201).json(...) [OK]
Hint: Use res.status(code).json(data) to set status and send JSON [OK]
Common Mistakes:
Using sendStatus then chaining json
Passing status code as first json argument
Using non-existent statusCode method
3. Given the following NestJS controller method, what will be the HTTP status code and response body sent to the client?
A. res.status must be called before returning from method
B. Missing await keyword before res.json
C. res.json should be replaced with res.sendStatus
D. Calling res.status after res.json causes no effect
Solution
Step 1: Understand Express response flow
Calling res.json() sends the response immediately and ends it.
Step 2: Analyze order of method calls
Calling res.status(200) after res.json() has no effect because response is already sent.
Final Answer:
Calling res.status after res.json causes no effect -> Option D
Quick Check:
Set status before sending response [OK]
Hint: Always set status before sending response [OK]
Common Mistakes:
Calling res.status after res.json
Thinking res.json needs await
Confusing res.sendStatus with res.json
5. You want to create a NestJS controller method that returns a 404 status with a JSON error message if a user is not found, but returns the user data with status 200 if found. Which code snippet correctly implements this response handling?
hard
A. async getUser(@Res() res, @Param('id') id: string) {
const user = await this.userService.findById(id);
if (!user) {
res.json({ error: 'User not found' }).status(404);
} else {
res.json(user).status(200);
}
}
B. async getUser(@Res() res, @Param('id') id: string) {
const user = await this.userService.findById(id);
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
return res.status(200).json(user);
}
C. async getUser(@Res() res, @Param('id') id: string) {
const user = await this.userService.findById(id);
if (!user) {
res.json({ error: 'User not found' });
res.status(404);
} else {
res.json(user);
res.status(200);
}
}
D. async getUser(@Res() res, @Param('id') id: string) {
const user = await this.userService.findById(id);
if (!user) {
res.sendStatus(404);
} else {
res.json(user);
}
}
Solution
Step 1: Check correct chaining of status and json
async getUser(@Res() res, @Param('id') id: string) {
const user = await this.userService.findById(id);
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
return res.status(200).json(user);
} correctly calls res.status(404).json(...) and res.status(200).json(...), chaining status and JSON response properly.
Step 2: Identify errors in other options
async getUser(@Res() res, @Param('id') id: string) {
const user = await this.userService.findById(id);
if (!user) {
res.json({ error: 'User not found' }).status(404);
} else {
res.json(user).status(200);
}
} calls res.json() before res.status(), which does not set status correctly. async getUser(@Res() res, @Param('id') id: string) {
const user = await this.userService.findById(id);
if (!user) {
res.json({ error: 'User not found' });
res.status(404);
} else {
res.json(user);
res.status(200);
}
} calls res.json() before res.status(), which does not set status correctly. async getUser(@Res() res, @Param('id') id: string) {
const user = await this.userService.findById(id);
if (!user) {
res.sendStatus(404);
} else {
res.json(user);
}
} uses res.sendStatus(404) which sends response immediately but does not send JSON error message.
Final Answer:
correctly sets status and sends JSON error or data -> Option B
Quick Check:
Chain status and json for correct response [OK]
Hint: Chain res.status(code).json(data) for clear responses [OK]
Common Mistakes:
Calling res.json before res.status
Using res.sendStatus without JSON body
Separating status and json calls without chaining
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.