模块、控制器与服务
深入了解核心组件:模块如何组织代码、控制器如何处理请求,以及服务如何封装业务逻辑。
模块、控制器与服务 是 CoddyKit 上的免费 NestJS Enterprise Backend APIs 课时。 这是第 3 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 NestJS Enterprise Backend APIs 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 NestJS Enterprise Backend APIs 课程共包含 3 节课。
本课时的部分内容尚未翻译,以英文显示。
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!
常见问题解答
「模块、控制器与服务」课时是免费的吗?
是的 — 「模块、控制器与服务」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 NestJS Enterprise Backend APIs 课程的其余内容,请升级到 CoddyKit PRO。 NestJS Enterprise Backend APIs 课程共包含 3 节课。
「模块、控制器与服务」这节课中我会学到什么?
深入了解核心组件:模块如何组织代码、控制器如何处理请求,以及服务如何封装业务逻辑。 你通过在浏览器中直接运行的动手代码来练习 NestJS Enterprise Backend APIs,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 NestJS Enterprise Backend APIs 需要有经验吗?
无需任何先前经验。CoddyKit 上的 NestJS Enterprise Backend APIs 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 3 节。
「模块、控制器与服务」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 NestJS Enterprise Backend APIs 课中编写并运行代码吗?
能。每节 NestJS Enterprise Backend APIs 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- NestJS 框架简介
- 项目结构与 CLI
- 模块、控制器与服务