Bird
Raised Fist0
NestJSframework~5 mins

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

Choose your learning style10 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.

      Practice

      (1/5)
      1. In NestJS, what does the HTTP status code 201 usually indicate in a response?
      easy
      A. A new resource was successfully created
      B. The request was malformed or invalid
      C. The requested resource was not found
      D. The server encountered an internal error

      Solution

      1. Step 1: Understand HTTP status codes

        HTTP status codes communicate the result of a client's request. Code 201 means "Created".
      2. Step 2: Match 201 with its meaning

        201 specifically means a new resource was successfully created on the server.
      3. Final Answer:

        A new resource was successfully created -> Option A
      4. Quick Check:

        201 = Created [OK]
      Hint: 201 means resource created successfully [OK]
      Common Mistakes:
      • Confusing 201 with 404 (not found)
      • Thinking 201 means server error
      • Mixing 201 with 400 (bad request)
      2. Which of the following is the correct way to set a custom header X-Custom-Header with value 123 in a NestJS controller using Express response?
      easy
      A. res.setHeader('X-Custom-Header', 789);
      B. res.set('X-Custom-Header', '123');
      C. res.header('X-Custom-Header', '789');
      D. res.addHeader('X-Custom-Header', '123');

      Solution

      1. Step 1: Recall Express response methods

        Express response object uses res.set() to set headers.
      2. Step 2: Check method and value types

        res.set() accepts header name and string value. res.set('X-Custom-Header', '123'); uses correct method and string value.
      3. Final Answer:

        res.set('X-Custom-Header', '123'); -> Option B
      4. Quick Check:

        Use res.set() to set headers [OK]
      Hint: Use res.set() to set headers in NestJS [OK]
      Common Mistakes:
      • Using res.setHeader (not Express method)
      • Passing number instead of string as header value
      • Using non-existent res.addHeader method
      3. What will be the HTTP status code and header in the response when this NestJS controller method runs?
      async getData(@Res() res) {
        res.status(204).set('X-Info', 'No Content').send();
      }
      medium
      A. Status 204 with no headers
      B. Status 200 with header X-Info: No Content
      C. Status 204 with header X-Info: No Content
      D. Status 404 with header X-Info: No Content

      Solution

      1. Step 1: Analyze status and header setting

        The code sets status to 204 and header 'X-Info' to 'No Content'.
      2. Step 2: Understand 204 status behavior

        Status 204 means "No Content" and allows custom headers. The header is set correctly.
      3. Final Answer:

        Status 204 with header X-Info: No Content -> Option C
      4. Quick Check:

        Status 204 + header set = Status 204 with header X-Info: No Content [OK]
      Hint: res.status(204).set() sets status and headers [OK]
      Common Mistakes:
      • Assuming 204 means no headers allowed
      • Confusing default status 200 with explicit 204
      • Ignoring header set after status
      4. Identify the error in this NestJS controller method that tries to set status and headers:
      async update(@Res() res) {
        res.status(200);
        res.set('Content-Type', 'application/json');
        res.send({ success: true });
        res.status(201);
      }
      medium
      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

      1. Step 1: Review response flow

        res.send() sends the response and ends it. Any code after it won't affect response.
      2. Step 2: Check status calls order

        Calling res.status(201) after res.send() does nothing because response is already sent.
      3. Final Answer:

        Calling res.status(201) after res.send() has no effect -> Option A
      4. 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

      1. Step 1: Order of method calls matters

        In Express, chaining status, set, and json works if status is set before sending.
      2. 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.
      3. 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.
      4. Final Answer:

        res.status(202).set('X-Request-ID', requestId).json({ message: 'Accepted' }); -> Option D
      5. 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 unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard