02 / 04

What is the role of a controller in NestJS?

Controllers handle incoming HTTP requests and return responses. They are decorated with @Controller('route-prefix') and their methods use HTTP-method decorators like @Get(), @Post(), etc. Controllers are thin — they delegate business logic to providers/services.

A controller is responsible for handling incoming requests and returning responses to the client. It should remain thin and delegate actual business logic to services.

Basic controller example
Key responsibilities of a controller:
  1. 1

    Map routes to handler methods using decorators.

  2. 2

    Extract data from the request (params, query, body, headers).

  3. 3

    Delegate logic to services.

  4. 4

    Return the response (NestJS handles serialization automatically).

Difficulty: 5/10
Topics: routing, request handling, dependency injection

Scenario Questions

0-2 years experience
  1. 1

    We need a new endpoint /users that returns a list of users; walk me through how you would create the controller and what its responsibilities are.

  2. 2

    A request hits a route defined in a controller but the response body is always empty; what might be missing in the controller method?

  3. 3

    How would you use decorators in a controller to extract query parameters and pass them to a service?

2-5 years experience
  1. 1

    Our controller calls several services and sometimes returns a 500 error; how would you structure error handling and status code mapping inside the controller?

  2. 2

    During a review you see a controller directly accessing the database; why is that a problem and how would you refactor it?

  3. 3

    If you need to protect all routes in a controller with authentication, what options do you have and what trade‑offs do they involve?

5-8 years experience
  1. 1

    Our API gateway forwards requests to multiple NestJS microservices; how would you design controllers to keep latency low and avoid coupling business logic to the controller layer?

  2. 2

    When scaling to thousands of concurrent requests, what considerations around async handling, interceptors, or pipes become critical in controller design?

  3. 3

    We need to version our API without duplicating controller code; how would you approach versioning at the controller level while keeping the codebase maintainable?

8+ years experience
  1. 1

    We are migrating a large legacy Express codebase to NestJS; how would you plan the transition of routing logic and controllers to minimize disruption across teams?

  2. 2

    Different teams have written controllers with varying patterns; what governance or architectural guidelines would you establish to standardize controller responsibilities and promote reuse?

  3. 3

    If we want to expose both REST and GraphQL endpoints for the same business operations, how would you structure controllers (or their equivalents) to avoid duplication and keep contracts clear?

Follow-up Questions

  • What would happen if you placed business logic directly inside a controller method?
  • How does NestJS instantiate controllers and what lifecycle hooks are available?
  • Can you explain how you would apply a global interceptor to all controllers?