Skip to content

Creating a Controller

This is the shortest path from nothing to a working route. For the concepts behind each step, see Routing & Controllers.

src/health/health.controller.ts
import { Controller, Get } from "@blixis-io/http";
@Controller("health")
export class HealthController {
@Get()
check() {
return { status: "ok" };
}
}

A controller with no constructor dependencies doesn’t even need @Injectable() — the container only reflects constructor parameters when there are any (see Dependency Injection). Once this controller needs a service, add one:

src/health/health.controller.ts
import { Controller, Get } from "@blixis-io/http";
import { HealthService } from "./health.service.js";
@Controller("health")
export class HealthController {
constructor(private readonly health: HealthService) {}
@Get()
check() {
return this.health.status();
}
}
src/health/health.module.ts
import { Module } from "@blixis-io/core";
import { HealthController } from "./health.controller.js";
import { HealthService } from "./health.service.js";
@Module({
providers: [HealthService],
controllers: [HealthController],
})
export class HealthModule {}

A controller listed in controllers but missing @Controller() fails fast with NotAControllerError when the app builds — you’ll know immediately, not at request time.

src/app.module.ts
@Module({
imports: [HealthModule /* , ...your other modules */],
})
export class AppModule {}
@Get(":id")
getOne(@Param("id") id: string) { /* ... */ }
@Post()
create(@Body(CreateSchema) input: CreateInput) { /* ... */ }

See Request Validation for @Body/@Query/@Param/@Headers, and Protecting Routes with Guards to add auth.