NestJS 微服务样板项目教程
1. 项目的目录结构及介绍
nestjs-boilerplate-microservice/
├── src/
│ ├── application/
│ ├── domain/
│ ├── infrastructure/
│ ├── presentation/
│ ├── shared/
│ │ ├── constants/
│ │ ├── decorators/
│ │ ├── exceptions/
│ │ ├── filters/
│ │ ├── guards/
│ │ ├── interceptors/
│ │ ├── middleware/
│ │ ├── pipes/
│ │ ├── strategies/
│ │ ├── utils/
│ │ └── validators/
│ ├── tests/
│ └── main.ts
├── test/
│ └── initialization.ts
├── TRACING.md
├── tsconfig.build.json
└── tsconfig.json
目录结构介绍
- src/application: 包含应用层代码,处理业务逻辑。
- src/domain: 包含领域层代码,定义领域模型和业务规则。
- src/infrastructure: 包含基础设施层代码,处理与外部系统的交互。
- src/presentation: 包含表示层代码,处理用户界面和API接口。
- src/shared: 包含共享模块,如常量、装饰器、异常处理等。
- src/tests: 包含测试代码。
- main.ts: 项目的入口文件。
- test/initialization.ts: 测试初始化文件。
- TRACING.md: 追踪文档。
- tsconfig.build.json: TypeScript 构建配置文件。
- tsconfig.json: TypeScript 配置文件。
2. 项目的启动文件介绍
main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();
启动文件介绍
- NestFactory.create(AppModule): 创建 NestJS 应用实例。
- app.listen(3000): 监听端口 3000。
3. 项目的配置文件介绍
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es2017",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true
},
"include": ["src/**/*"]
}
tsconfig.build.json
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}
配置文件介绍
- tsconfig.json: TypeScript 编译选项,包括模块系统、目标版本、输出目录等。
- tsconfig.build.json: 继承
tsconfig.json
,排除node_modules
、test
、dist
和测试文件。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考