Bird
Raised Fist0
NestJSframework~8 mins

Status codes and headers in NestJS - Performance & Optimization

Choose your learning style10 modes available
Performance: Status codes and headers
MEDIUM IMPACT
This affects how quickly the browser understands the server response and how efficiently it processes caching and content rendering.
Sending HTTP responses with correct status codes and caching headers
NestJS
res.status(200).set({ 'Cache-Control': 'public, max-age=3600' }).send(data);
Correct status codes and caching headers reduce redundant network requests and speed up content loading.
📈 Performance GainReduces network round-trips and improves LCP by enabling browser caching
Sending HTTP responses with correct status codes and caching headers
NestJS
res.status(200).send(data); // No caching headers or incorrect status code for errors
Missing or incorrect status codes and headers cause browsers to re-fetch resources unnecessarily or misinterpret response meaning.
📉 Performance CostIncreases network requests and delays LCP due to lack of caching and error handling
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
No caching headers, incorrect status codesN/A00[X] Bad
Correct status codes with caching headersN/A00[OK] Good
Rendering Pipeline
Status codes and headers are processed early by the browser to decide how to handle the response, affecting caching, rendering, and error handling.
→Network
→Cache Lookup
→Rendering
⚠️ BottleneckNetwork round-trips caused by missing or incorrect caching headers
Core Web Vital Affected
LCP
This affects how quickly the browser understands the server response and how efficiently it processes caching and content rendering.
Optimization Tips
1Always use correct HTTP status codes to communicate response meaning.
2Set Cache-Control headers to enable efficient browser caching.
3Avoid sending unnecessary headers that increase response size.
Performance Quiz - 3 Questions
Test your performance knowledge
How do correct HTTP status codes affect web performance?
AThey increase the size of the response, slowing down loading.
BThey help browsers understand response meaning and avoid unnecessary retries.
CThey only affect server logs and have no impact on performance.
DThey force browsers to ignore caching headers.
DevTools: Network
How to check: Open DevTools > Network tab, reload the page, click a resource, and inspect the Status and Response Headers sections.
What to look for: Look for correct HTTP status codes (200, 304, 404, etc.) and caching headers like Cache-Control or Expires to confirm efficient response handling.

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