0
0
NestJSframework~5 mins

Response handling in NestJS - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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.
Which decorator sets a custom HTTP status code for a response in NestJS?
A@ResponseStatus()
B@StatusCode()
C@HttpCode()
D@SetStatus()
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.
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.
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.
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.