Controllers

Controllers are classes decorated with @Controller that handle incoming HTTP requests. Each method in a controller can be mapped to a specific HTTP verb and route.

@Controller(prefix)

Marks a class as a controller and sets the route prefix for all methods inside it.

Parameter Type Required Description
prefix string Required The base URL prefix for all routes in this controller (e.g. '/users').
user.controller.ts TypeScript
import { Controller, Get, Post, Put, Delet } from '@abrahambass/nova';

@Controller('/users')
class UserController {

  @Get()                      // GET /users
  async findAll() { ... }

  @Get('/:id')               // GET /users/:id
  async findOne() { ... }

  @Post()                     // POST /users
  async create() { ... }

  @Put('/:id')               // PUT /users/:id
  async update() { ... }

  @Delet('/:id')             // DELETE /users/:id
  async remove() { ... }
}
💡 Note

The @Controller decorator automatically makes the class injectable (via Inversify). You don't need to add @injectable() manually.

Registering Controllers

Controllers must be registered with the application. There are two ways:

1. Directly with the App

main.ts TypeScript
app.includeController(UserController);

2. Via a Module

user.module.ts TypeScript
import { Module } from '@abrahambass/nova';
import { UserController } from './user.controller';

export const UserModule = new Module()
  .registerController(UserController);

Injecting Services

Controllers support constructor injection. Use @inject from Inversify to inject services:

user.controller.ts TypeScript
import { Controller, Get, inject } from '@abrahambass/nova';
import { UserService } from './user.service';

@Controller('/users')
class UserController {
  constructor(
    @inject(UserService) private userService: UserService,
  ) {}

  @Get()
  async findAll() {
    return this.userService.findAll();
  }
}