0
0
NestJSframework~20 mins

Response handling in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Response Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the HTTP status code returned by this NestJS controller method?
Consider the following NestJS controller method. What HTTP status code will the client receive when this endpoint is called?
NestJS
import { Controller, Get, Res } from '@nestjs/common';
import { Response } from 'express';

@Controller('items')
export class ItemsController {
  @Get()
  getItems(@Res() res: Response) {
    res.json({ message: 'List of items' });
  }
}
A200 OK
B201 Created
C204 No Content
D500 Internal Server Error
Attempts:
2 left
💡 Hint
When you use res.json() without setting status, what is the default HTTP status code?
📝 Syntax
intermediate
2:00remaining
Which option correctly sets a custom HTTP header in a NestJS response?
You want to add a custom header 'X-Custom-Header' with value 'NestJS' to the HTTP response. Which code snippet correctly does this?
NestJS
import { Controller, Get, Res } from '@nestjs/common';
import { Response } from 'express';

@Controller('test')
export class TestController {
  @Get('header')
  setHeader(@Res() res: Response) {
    // Add custom header here
    res.send('Header set');
  }
}
Ares.append('X-Custom-Header', 'NestJS');
Bres.set('X-Custom-Header', 'NestJS');
Cres.addHeader('X-Custom-Header', 'NestJS');
Dres.header('X-Custom-Header', 'NestJS');
Attempts:
2 left
💡 Hint
Check Express response methods for setting headers.
state_output
advanced
2:00remaining
What will be the JSON response body from this NestJS controller method?
Analyze the following NestJS controller method. What JSON will the client receive?
NestJS
import { Controller, Get } from '@nestjs/common';

@Controller('users')
export class UsersController {
  @Get('profile')
  getProfile() {
    return { id: 1, name: 'Alice', active: true };
  }
}
A{"id":1,"name":"Alice","active":true}
B"{id:1, name:'Alice', active:true}"
C{"id":"1","name":"Alice","active":"true"}
D{"id":1,"name":"Alice"}
Attempts:
2 left
💡 Hint
What does returning a plain object from a NestJS controller method do?
🔧 Debug
advanced
2:00remaining
Why does this NestJS controller method cause a runtime error?
Examine the code below. When calling this endpoint, a runtime error occurs. What is the cause?
NestJS
import { Controller, Get, Res } from '@nestjs/common';
import { Response } from 'express';

@Controller('orders')
export class OrdersController {
  @Get('list')
  listOrders(@Res() res: Response) {
    res.json({ orders: [] });
  }
}
Ares.json() is not a function on Response object.
BMissing await keyword before res.json() causes error.
CUsing both return and res.json() causes NestJS to send response twice.
DThe method lacks a @Post decorator causing mismatch.
Attempts:
2 left
💡 Hint
Check how NestJS handles responses when @Res() is used with return statements.
🧠 Conceptual
expert
3:00remaining
Which option correctly describes how NestJS handles response serialization by default?
In NestJS, when a controller method returns a plain object, how does NestJS handle the response serialization and what can you do to customize it?
ANestJS requires you to always use @Res() and call res.json() to send JSON responses.
BNestJS sends returned objects as raw JavaScript without serialization; you must manually call JSON.stringify().
CNestJS converts returned objects to XML by default; JSON serialization requires extra setup.
DNestJS automatically serializes returned objects to JSON using class-transformer; you can customize serialization with @Exclude and @Expose decorators.
Attempts:
2 left
💡 Hint
Think about how NestJS integrates with class-transformer for response objects.