NestJS 11 · Controllers, providers and modules

Write a provider (service)

@Injectable classes are created and injected by Nest. Throw built-in HTTP exceptions for errors.

@Injectable()
export class CatsService {
  private cats: Cat[] = [];

  findOne(id: number): Cat {
    const cat = this.cats.find((c) => c.id === id);
    if (!cat) throw new NotFoundException(`Cat ${id} not found`);
    return cat;
  }
}

Official NestJS 11 documentation

More controllers, providers and modules commands

All controllers, providers and modules commands · NestJS 11 cheatsheet

Was this page helpful?