Modules, Controllers, Services
Dive into the core components: how modules organize code, controllers handle requests, and services encapsulate business logic.
Modules, Controllers, Services is a free NestJS Enterprise Backend APIs lesson on CoddyKit — lesson 3 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the NestJS Enterprise Backend APIs learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
NestJS Building Blocks
A NestJS app is built from three core blocks — Modules, Controllers, and Services — that organize code, handle requests, and hold logic.
Organizing with Modules
A Module (class with @Module()) is a feature group: it bundles related controllers and services and defines their dependency scope.
Your First NestJS Module
This AppModule shows the @Module() decorator declaring a controller and a service, making them available within its scope.
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
@Module({
imports: [], // Other modules this module needs
controllers: [AppController], // Controllers handled by this module
providers: [AppService], // Services (providers) available in this module
exports: [], // Providers to export for other modules
})
export class AppModule {}Controllers: Handling Requests
Controllers (classes with @Controller()) handle incoming HTTP requests and return responses, delegating the heavy logic to services.
A Simple API Endpoint
This AppController uses @Get() to handle GET requests at /, calling AppService for the actual message.
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller() // Optional path prefix, e.g., '@Controller('users')'
export class AppController {
constructor(private readonly appService: AppService) {}
@Get() // Handles GET requests to '/' (or '/users' if prefix exists)
getHello(): string {
return this.appService.getHello();
}
}Services: Business Logic Hub
Services (providers marked @Injectable()) hold your business logic and DB calls — reusable units injected into controllers to keep them lean.
Your Logic in a Service
This AppService exposes a method for the controller to call. The @Injectable() decorator lets NestJS manage it via DI.
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getHello(): string {
return 'Hello from NestJS!';
}
}Connecting the Dots
Dependency Injection wires it together: declare the parts in a module, ask for the service in the controller's constructor, and Nest injects it.
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
console.log('NestJS application is running on http://localhost:3000');
}
bootstrap();
// When you visit http://localhost:3000,
// AppController's getHello() is called,
// which in turn calls AppService's getHello().Component Roles Check
You've learned about Modules, Controllers, and Services. How well do you understand their primary roles?
Core Components Summary
You learned the core trio: Modules organize, Controllers handle requests, Services hold logic — all connected through DI. TypeORM next!
Frequently asked questions
Is the “Modules, Controllers, Services” lesson free?
Yes — the full text of “Modules, Controllers, Services” is free to read here on the web, and the NestJS Enterprise Backend APIs course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the NestJS Enterprise Backend APIs course, upgrade to CoddyKit PRO.
What will I learn in “Modules, Controllers, Services”?
Dive into the core components: how modules organize code, controllers handle requests, and services encapsulate business logic. You practise NestJS Enterprise Backend APIs with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start NestJS Enterprise Backend APIs?
No prior experience is required. NestJS Enterprise Backend APIs on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “Modules, Controllers, Services” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this NestJS Enterprise Backend APIs lesson?
Yes. Every NestJS Enterprise Backend APIs lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to NestJS Framework
- Project Structure and CLI
- Modules, Controllers, Services