Challenge - 5 Problems
Controller Decorator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the route path for this controller?
Given the following NestJS controller code, what is the base route path that this controller will handle?
NestJS
import { Controller, Get } from '@nestjs/common'; @Controller('users') export class UsersController { @Get() findAll() { return 'List of users'; } }
Attempts:
2 left
💡 Hint
The string inside @Controller defines the base route path for all methods inside the controller.
✗ Incorrect
The @Controller('users') decorator sets the base route path to /users. The @Get() decorator on findAll() means it handles GET requests at the base path, so the full route is /users.
📝 Syntax
intermediate2:00remaining
Which option causes a syntax error in this controller?
Identify which controller code snippet will cause a syntax error in NestJS.
Attempts:
2 left
💡 Hint
Check if the decorator syntax is correct with parentheses.
✗ Incorrect
Option A misses parentheses after @Get. Decorators must be called as functions, so @Get() is required. This causes a syntax error.
❓ state_output
advanced2:00remaining
What is the output when accessing /api/items?
Consider this NestJS controller. What will be the response body when a GET request is made to '/api/items'?
NestJS
import { Controller, Get } from '@nestjs/common'; @Controller('api/items') export class ItemsController { @Get() getItems() { return { count: 3, items: ['a', 'b', 'c'] }; } }
Attempts:
2 left
💡 Hint
The method returns an object literal which NestJS serializes to JSON.
✗ Incorrect
The getItems() method returns an object with count and items. NestJS automatically converts this to JSON in the HTTP response.
🔧 Debug
advanced2:00remaining
Why does this controller method never get called?
Given this controller, why does the method not respond to any HTTP requests?
NestJS
import { Controller, Get } from '@nestjs/common'; @Controller('tasks') export class TasksController { getTasks() { return 'Task list'; } }
Attempts:
2 left
💡 Hint
Check if the method has the correct HTTP method decorator.
✗ Incorrect
In NestJS, methods must have decorators like @Get() to handle HTTP requests. Without it, the method is not exposed as a route.
🧠 Conceptual
expert3:00remaining
What is the effect of multiple @Controller decorators on one class?
In NestJS, what happens if a class is decorated with multiple @Controller decorators like this?
@Controller('beta')
@Controller('alpha')
export class MultiController {
@Get()
getData() {
return 'data';
}
}Attempts:
2 left
💡 Hint
Consider how decorators are applied in TypeScript and NestJS.
✗ Incorrect
In TypeScript, decorators are applied bottom-up. The last @Controller decorator overwrites the previous one. So only '/beta' is used as the base path.