Challenge - 5 Problems
Response Handling Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2: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' }); } }
Attempts:
2 left
💡 Hint
When you use res.json() without setting status, what is the default HTTP status code?
✗ Incorrect
Using res.json() sends a JSON response with status code 200 by default.
📝 Syntax
intermediate2: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'); } }
Attempts:
2 left
💡 Hint
Check Express response methods for setting headers.
✗ Incorrect
The res.set() method sets HTTP headers correctly. res.header() is an alias but not recommended in NestJS context. res.addHeader() does not exist. res.append() appends values to existing headers.
❓ state_output
advanced2: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 }; } }
Attempts:
2 left
💡 Hint
What does returning a plain object from a NestJS controller method do?
✗ Incorrect
Returning a plain object from a controller method sends it as JSON with correct types preserved.
🔧 Debug
advanced2: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: [] }); } }
Attempts:
2 left
💡 Hint
Check how NestJS handles responses when @Res() is used with return statements.
✗ Incorrect
When using @Res() and calling res methods directly, you should not return a value. Returning causes NestJS to try sending response again, leading to an error.
🧠 Conceptual
expert3: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?
Attempts:
2 left
💡 Hint
Think about how NestJS integrates with class-transformer for response objects.
✗ Incorrect
By default, NestJS uses class-transformer to serialize returned objects to JSON. You can control which properties are included using decorators like @Exclude and @Expose.