Bird
Raised Fist0
NestJSframework~20 mins

Controller decorator in NestJS - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of the @Controller decorator in NestJS?
easy
A. To define a service that handles business logic
B. To mark a class as a group of routes with a base path
C. To create a database connection
D. To style the HTML output of a component

Solution

  1. Step 1: Understand the role of @Controller

    The @Controller decorator marks a class as a handler for HTTP requests grouped under a base path.
  2. Step 2: Differentiate from other decorators

    It is not used for services, database, or styling but specifically for routing.
  3. Final Answer:

    To mark a class as a group of routes with a base path -> Option B
  4. Quick Check:

    @Controller = route group [OK]
Hint: Remember: @Controller groups routes under one path [OK]
Common Mistakes:
  • Confusing @Controller with @Injectable
  • Thinking it styles components
  • Assuming it manages database connections
2. Which of the following is the correct syntax to define a controller with base path 'users' in NestJS?
easy
A. @Controller = 'users' export class UsersController {}
B. @Controller.users export class UsersController {}
C. @Controller('users') export class UsersController {}
D. @Controller path='users' export class UsersController {}

Solution

  1. Step 1: Recall correct decorator syntax

    The @Controller decorator takes the base path as a string argument inside parentheses.
  2. Step 2: Check each option's syntax

    Only @Controller('users') export class UsersController {} uses the correct syntax: @Controller('users'). Others misuse dot, equal sign, or invalid syntax.
  3. Final Answer:

    @Controller('users') export class UsersController {} -> Option C
  4. Quick Check:

    @Controller('path') syntax = @Controller('users') export class UsersController {} [OK]
Hint: Use parentheses with string path in @Controller [OK]
Common Mistakes:
  • Using dot notation instead of parentheses
  • Assigning with equal sign instead of decorator call
  • Writing invalid decorator syntax
3. Given the following controller code, what is the full URL path to access the getAllUsers method?
@Controller('api')
export class UserController {
  @Get('users')
  getAllUsers() {
    return 'List of users';
  }
}
medium
A. /users
B. /users/api
C. /api/getAllUsers
D. /api/users

Solution

  1. Step 1: Identify base path from @Controller

    The base path is 'api' from @Controller('api').
  2. Step 2: Identify route path from @Get decorator

    The method route is 'users' from @Get('users').
  3. Step 3: Combine base and method paths

    Full path is base + method = '/api/users'.
  4. Final Answer:

    /api/users -> Option D
  5. Quick Check:

    Base + method path = /api/users [OK]
Hint: Combine @Controller and @Get paths for full URL [OK]
Common Mistakes:
  • Reversing base and method paths
  • Using method name as URL path
  • Ignoring base path in URL
4. Identify the error in this NestJS controller code:
@Controller('products')
export class ProductController {
  @Get()
  getAll() {
    return 'All products';
  }

  @Get('')
  getEmpty() {
    return 'Empty route';
  }
}
medium
A. Duplicate routes for the same path cause a conflict
B. Missing parentheses in @Get decorator
C. Controller path should be '/products' with a slash
D. Methods must return JSON objects, not strings

Solution

  1. Step 1: Analyze route paths from @Get decorators

    Both methods respond to the same route: base 'products' + '' or no path means '/products'.
  2. Step 2: Understand route conflict

    Having two methods for the same route causes a conflict and runtime error.
  3. Final Answer:

    Duplicate routes for the same path cause a conflict -> Option A
  4. Quick Check:

    Duplicate routes = conflict error [OK]
Hint: Avoid duplicate routes with same path in one controller [OK]
Common Mistakes:
  • Focusing on @Get parentheses instead of duplicate routes
  • Believing controller path needs leading slash
  • Assuming string return type is invalid
5. You want to create a controller that handles routes for both '/admin' and '/user' paths using a single class. Which approach correctly uses the @Controller decorator to achieve this?
hard
A. Create two separate controllers, one for 'admin' and one for 'user'
B. @Controller(['admin', 'user']) export class MultiPathController {}
C. @Controller('admin,user') export class MultiPathController {}
D. @Controller('admin') @Controller('user') export class MultiPathController {}

Solution

  1. Step 1: Understand @Controller supports single base path

    The @Controller decorator accepts a single string as the base path. It does not support multiple paths in an array.
  2. Step 2: Evaluate options for multiple paths

    Create two separate controllers, one for 'admin' and one for 'user' is correct because creating two separate controllers is the proper way to handle multiple base paths. @Controller(['admin', 'user']) export class MultiPathController {} is invalid syntax. @Controller('admin,user') export class MultiPathController {} uses a comma-separated string which is treated as a single path. @Controller('admin') @Controller('user') export class MultiPathController {} uses multiple decorators which is invalid.
  3. Final Answer:

    Create two separate controllers, one for 'admin' and one for 'user' -> Option A
  4. Quick Check:

    One controller per base path [OK]
Hint: Use separate controllers for different base paths [OK]
Common Mistakes:
  • Using array of strings in @Controller (not supported)
  • Applying multiple @Controller decorators on one class
  • Trying to combine multiple paths in one string
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard