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()
✗ Incorrect
The @HttpCode() decorator is used to set the HTTP status code for a controller method in NestJS.
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
✗ Incorrect
You add custom headers by injecting the Response object with @Res() and calling res.set() or res.header() before sending the response.
What status code does NestJS return by default for a successful GET request?
A201 Created
B200 OK
C204 No Content
D400 Bad Request
✗ Incorrect
By default, NestJS returns 200 OK for successful GET requests.
If you use @Res() in a controller method, what must you remember to do?
AManually send the response using res.send() or res.json()
A. Calling res.status(201) after res.send() has no effect
B. res.set() must be called before res.status()
C. res.send() cannot send JSON objects
D. res.status() cannot be called twice
Solution
Step 1: Review response flow
res.send() sends the response and ends it. Any code after it won't affect response.
Step 2: Check status calls order
Calling res.status(201) after res.send() does nothing because response is already sent.
Final Answer:
Calling res.status(201) after res.send() has no effect -> Option A
Quick Check:
res.send() ends response, later status calls ignored [OK]
Hint: Set status before res.send(), not after [OK]
Common Mistakes:
Trying to set status after sending response
Thinking res.send() can't send JSON
Believing res.set() must come before res.status()
5. You want to send a 202 Accepted status with a custom header X-Request-ID and a JSON body in NestJS. Which code snippet correctly achieves this?
hard
A. res.send({ message: 'Accepted' }).status(202).set('X-Request-ID', requestId);
B. res.set('X-Request-ID', requestId).status(202).send('Accepted');
C. res.status(202).send({ message: 'Accepted' }).set('X-Request-ID', requestId);
D. res.status(202).set('X-Request-ID', requestId).json({ message: 'Accepted' });
Solution
Step 1: Order of method calls matters
In Express, chaining status, set, and json works if status is set before sending.
Step 2: Check each option's correctness
Options A and C call send before completing status/header setup, invalid. res.set('X-Request-ID', requestId).status(202).send('Accepted'); sets header and status but uses res.send('Accepted') sending plain text instead of JSON object. res.status(202).set('X-Request-ID', requestId).json({ message: 'Accepted' }); correctly sets status, header, sends JSON.
Step 3: Confirm best practice
res.status(202).set('X-Request-ID', requestId).json({ message: 'Accepted' }); is the clearest and most common pattern: status, set header, then send JSON.
Final Answer:
res.status(202).set('X-Request-ID', requestId).json({ message: 'Accepted' }); -> Option D
Quick Check:
Status then headers then send JSON [OK]
Hint: Chain status, set, then send/json in that order [OK]
Common Mistakes:
Calling send before setting status or headers
Using send instead of json for JSON body
Setting headers after sending response
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.