Bird
Raised Fist0
NestJSframework~20 mins

Status codes and headers in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Status Code & Header Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What status code and header does this NestJS controller send?
Consider this NestJS controller method. What status code and header will the client receive?
NestJS
import { Controller, Get, Res, HttpStatus } from '@nestjs/common';
import { Response } from 'express';

@Controller('test')
export class TestController {
  @Get()
  getData(@Res() res: Response) {
    res.status(HttpStatus.CREATED).set('X-Custom-Header', 'NestJS').json({ message: 'Created' });
  }
}
AStatus code 201 with no custom headers and JSON body { message: 'Created' }
BStatus code 200 with header 'X-Custom-Header: NestJS' and JSON body { message: 'Created' }
CStatus code 201 with header 'X-Custom-Header: NestJS' and JSON body { message: 'Created' }
DStatus code 200 with no custom headers and JSON body { message: 'Created' }
Attempts:
2 left
💡 Hint
Look at the status() and set() methods on the response object.
📝 Syntax
intermediate
2:00remaining
Which option correctly sets a 404 status with a custom header in NestJS?
You want to send a 404 Not Found status with a header 'X-Error: NotFound'. Which code snippet is correct?
Ares.status(404).set('X-Error', 'NotFound').send('Not Found');
Bres.status(HttpStatus.NOT_FOUND).header('X-Error', 'NotFound').send('Not Found');
Cres.status(HttpStatus.NOT_FOUND).setHeader('X-Error', 'NotFound').send('Not Found');
Dres.status(404).setHeaders('X-Error', 'NotFound').send('Not Found');
Attempts:
2 left
💡 Hint
Check the Express Response methods used in NestJS.
🔧 Debug
advanced
2:00remaining
Why does this NestJS controller not send the custom header?
This controller method tries to send a custom header but it does not appear in the response. Why?
NestJS
import { Controller, Get, Res } from '@nestjs/common';
import { Response } from 'express';

@Controller('debug')
export class DebugController {
  @Get()
  sendHeader(@Res() res: Response) {
    res.set('X-Debug', 'true');
    return { success: true };
  }
}
ABecause the controller method must be async to set headers properly.
BBecause 'set' is not a valid method to set headers on the response object.
CBecause the header name 'X-Debug' is reserved and ignored by browsers.
DBecause returning an object bypasses the manual response, so headers set on res are ignored.
Attempts:
2 left
💡 Hint
Think about how NestJS handles @Res() and returned values.
❓ state_output
advanced
2:00remaining
What headers and status code are sent by this NestJS interceptor?
Given this interceptor that modifies the response, what headers and status code will the client receive?
NestJS
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable, map } from 'rxjs';

@Injectable()
export class HeaderInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    const response = context.switchToHttp().getResponse();
    response.status(202);
    response.set('X-Intercepted', 'yes');
    return next.handle().pipe(
      map(data => ({ ...data, intercepted: true }))
    );
  }
}
AStatus code 202 with header 'X-Intercepted: yes' and JSON body including intercepted: true
BStatus code 200 with header 'X-Intercepted: yes' and original JSON body
CStatus code 202 with no custom headers and JSON body including intercepted: true
DStatus code 200 with no custom headers and original JSON body
Attempts:
2 left
💡 Hint
Look at how the interceptor sets status and headers before passing control.
🧠 Conceptual
expert
3:00remaining
Which statement about setting status codes and headers in NestJS is true?
Choose the correct statement about how NestJS handles status codes and headers when using @Res() and returning values.
ANestJS automatically merges headers set by res.set() with headers from returned objects, so both are always sent.
BIf you use @Res() and call res.status() and res.set(), you must not return a value from the method or NestJS will ignore the manual response.
CYou can only set status codes using decorators like @HttpCode; res.status() is ignored in NestJS.
DHeaders set in middleware are overwritten by headers set in controllers, so middleware headers never reach the client.
Attempts:
2 left
💡 Hint
Think about how NestJS handles manual response control with @Res().

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