Bird
Raised Fist0
NestJSframework~5 mins

Controller decorator in NestJS - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What is the purpose of the @Controller decorator in NestJS?
The <code>@Controller</code> decorator marks a class as a controller that can handle incoming HTTP requests and send responses. It groups related request handlers under a common route prefix.
Click to reveal answer
beginner
How do you specify a route prefix using the @Controller decorator?
You pass a string to @Controller('prefix'). This prefix is added before all routes defined in the controller's methods.
Click to reveal answer
beginner
Can a NestJS controller class have multiple route handlers? How?
Yes. Inside a controller class, you define multiple methods decorated with HTTP method decorators like @Get(), @Post(), etc. Each method handles a different route or HTTP verb.
Click to reveal answer
beginner
What happens if you use @Controller() without any prefix string?
The controller will handle routes starting from the root path /. Its methods define the full route paths.
Click to reveal answer
intermediate
How does NestJS use the @Controller decorator internally?
NestJS uses <code>@Controller</code> to register the class as a controller in its routing system. It reads the decorator metadata to map HTTP requests to the controller's methods.
Click to reveal answer
What does the @Controller('users') decorator do?
AImports the 'users' module
BSets the route prefix to '/users' for all routes in the controller
CCreates a database table called 'users'
DDefines a service named 'users'
Which decorator is used inside a controller to handle GET requests?
A@Get()
B@Post()
C@Put()
D@Delete()
If a controller has @Controller() with no prefix, what is the base route?
A/
B/controller
C/api
D/home
Can a single NestJS controller handle multiple HTTP methods?
AOnly if you use multiple <code>@Controller</code> decorators
BNo, one controller handles only one HTTP method
CYes, by defining multiple methods with different HTTP decorators
DOnly if you extend another controller
What is the main role of the @Controller decorator in NestJS?
ATo define a database schema
BTo configure the server port
CTo create a middleware
DTo register a class as a route handler
Explain how the @Controller decorator works in NestJS and how it helps organize routes.
Think about how a controller groups related web routes under one roof.
You got /4 concepts.
    Describe how you would create a NestJS controller that handles GET requests at '/products' and POST requests at '/products'.
    Focus on the decorator usage for the class and methods.
    You got /4 concepts.

      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