0
0
NestJSframework~20 mins

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

Choose your learning style9 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().