0
0
NestJSframework~5 mins

Status codes and headers in NestJS - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is the purpose of HTTP status codes in NestJS?
HTTP status codes tell the client if a request was successful or if there was an error. In NestJS, they help communicate the result of an API call clearly.
Click to reveal answer
beginner
How do you set a custom HTTP status code in a NestJS controller?
Use the @HttpCode() decorator above the controller method to set a custom status code, for example, @HttpCode(201) sets the status to 201 Created.
Click to reveal answer
beginner
What is the role of HTTP headers in NestJS responses?
HTTP headers carry extra information about the response, like content type or caching rules. In NestJS, you can set headers to control how clients handle the response.
Click to reveal answer
intermediate
How can you set HTTP headers in a NestJS controller method?
You can inject the Response object from Express using @Res() and then use res.set() or res.header() to add headers before sending the response.
Click to reveal answer
intermediate
What is the difference between using @Res() and returning a value directly in NestJS controllers regarding status codes and headers?
Using @Res() gives full control over the response, including headers and status codes, but you must manually send the response. Returning a value lets NestJS handle status codes and headers automatically based on decorators.
Click to reveal answer
Which decorator sets the HTTP status code for a NestJS controller method?
A@SetStatus()
B@Status()
C@ResponseCode()
D@HttpCode()
How do you add a custom header to a response in NestJS?
AUse res.set() on the injected Response object
BUse @Header() decorator on the controller method
CReturn an object with headers property
DNestJS sets headers automatically; you cannot add custom ones
What status code does NestJS return by default for a successful GET request?
A201 Created
B200 OK
C204 No Content
D400 Bad Request
If you use @Res() in a controller method, what must you remember to do?
AManually send the response using res.send() or res.json()
BReturn the response object from the method
CUse @HttpCode() to set status code
DNothing special; NestJS handles response automatically
Which header is commonly used to specify the type of content returned by a NestJS API?
AAccept-Encoding
BAuthorization
CContent-Type
DCache-Control
Explain how to set both a custom status code and headers in a NestJS controller method.
Think about decorators and direct response manipulation.
You got /4 concepts.
    Describe the difference between returning a value directly and using @Res() in NestJS controllers regarding response control.
    Consider who controls the response sending.
    You got /4 concepts.