0
0
NestJSframework~20 mins

Controller decorator in NestJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Controller Decorator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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';
  }
}
A/users/findAll
B/
C/users
D/getUsers
Attempts:
2 left
💡 Hint
The string inside @Controller defines the base route path for all methods inside the controller.
📝 Syntax
intermediate
2:00remaining
Which option causes a syntax error in this controller?
Identify which controller code snippet will cause a syntax error in NestJS.
A
import { Controller, Get } from '@nestjs/common';

@Controller('orders')
export class OrdersController {
  @Get()
  getOrders() {
    return 'Orders list';
  }
}
B
import { Controller, Get } from '@nestjs/common';

@Controller('customers')
export class CustomersController {
  @Get()
  fetch() {
    return 'Customer data';
  }
}
C
import { Controller, Get } from '@nestjs/common';

@Controller('categories')
export class CategoriesController {
  @Get()
  list() {
    return 'Category list';
  }
}
D
import { Controller, Get } from '@nestjs/common';

@Controller('products')
export class ProductsController {
  @Get()
  getAll() {
    return 'All products';
  }
}
Attempts:
2 left
💡 Hint
Check if the decorator syntax is correct with parentheses.
state_output
advanced
2: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'] };
  }
}
A["a","b","c"]
B{"count":3,"items":["a","b","c"]}
C3
DError: Route not found
Attempts:
2 left
💡 Hint
The method returns an object literal which NestJS serializes to JSON.
🔧 Debug
advanced
2: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';
  }
}
AThe controller class is missing the @Injectable() decorator.
BThe controller path 'tasks' is invalid and blocks all routes.
CThe method name getTasks is reserved and ignored by NestJS.
DThe method is missing the @Get() decorator, so it is not registered as a route handler.
Attempts:
2 left
💡 Hint
Check if the method has the correct HTTP method decorator.
🧠 Conceptual
expert
3: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';
    }
  }
AOnly the last @Controller decorator ('beta') is applied, so the base path is '/beta'.
BThe class will handle routes for both '/alpha' and '/beta' base paths.
CNestJS throws a runtime error due to multiple @Controller decorators.
DThe class is ignored and no routes are registered.
Attempts:
2 left
💡 Hint
Consider how decorators are applied in TypeScript and NestJS.