Challenge - 5 Problems
NestJS Route Handler Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this GET route handler?
Consider this NestJS controller method handling a GET request. What will be the JSON response when a client calls this route?
NestJS
import { Controller, Get } from '@nestjs/common'; @Controller('items') export class ItemsController { @Get() getAll() { return { message: 'List of items', count: 3 }; } }
Attempts:
2 left
💡 Hint
Look at what the method returns and how NestJS sends JSON responses.
✗ Incorrect
The method returns an object with two properties. NestJS automatically converts this object to JSON in the HTTP response.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a POST route handler in NestJS?
You want to create a POST route handler in a NestJS controller. Which code snippet is syntactically correct?
NestJS
import { Controller, Post } from '@nestjs/common'; @Controller('users') export class UsersController { // Your POST handler here }
Attempts:
2 left
💡 Hint
Check the decorator syntax and method declaration carefully.
✗ Incorrect
The @Post decorator requires parentheses even if no path is given. The method must have parentheses after its name.
❓ state_output
advanced2:00remaining
What is the response after calling this PUT route handler?
Given this NestJS PUT route handler, what will be the JSON response when a client sends a PUT request?
NestJS
import { Controller, Put, Body } from '@nestjs/common'; @Controller('products') export class ProductsController { private products = [{ id: 1, name: 'Book' }]; @Put(':id') updateProduct(@Body() body: { name: string }) { this.products[0].name = body.name; return this.products[0]; } }
Attempts:
2 left
💡 Hint
The method updates the product name with the value from the request body.
✗ Incorrect
The method changes the first product's name to the value sent in the request body and returns the updated product.
🔧 Debug
advanced2:00remaining
Which option causes an error in this DELETE route handler?
This NestJS controller has a DELETE route handler. Which option will cause a runtime error when the route is called?
NestJS
import { Controller, Delete, Param } from '@nestjs/common'; @Controller('tasks') export class TasksController { private tasks = [{ id: 1, title: 'Task 1' }]; @Delete(':id') deleteTask(@Param('id') id: string) { const index = this.tasks.findIndex(task => task.id === Number(id)); if (index === -1) { throw new Error('Task not found'); } this.tasks.splice(index, 1); return { message: 'Deleted' }; } }
Attempts:
2 left
💡 Hint
Consider what happens when Number(id) is NaN and how findIndex behaves.
✗ Incorrect
When id is 'abc', Number(id) is NaN. The findIndex comparison fails to find a match, returning -1. The code throws 'Task not found' error, not a TypeError.
🧠 Conceptual
expert2:00remaining
Which HTTP method is best suited for updating part of a resource in NestJS?
In NestJS, you want to update only some fields of an existing resource without replacing it entirely. Which HTTP method and decorator should you use?
Attempts:
2 left
💡 Hint
Think about the difference between PUT and PATCH in HTTP methods.
✗ Incorrect
PUT replaces the entire resource, while PATCH updates only specified fields. NestJS supports both with @Put() and @Patch() decorators.