NestJS Boilerplate 项目教程
项目地址:https://gitcode.com/gh_mirrors/ne/nestjs-boilerplate
1. 项目的目录结构及介绍
NestJS Boilerplate 项目的目录结构如下:
nestjs-boilerplate/
├── src/
│ ├── auth/
│ ├── common/
│ ├── config/
│ ├── health/
│ ├── mail/
│ ├── notification/
│ ├── prisma/
│ ├── seed/
│ ├── user/
│ ├── app.module.ts
│ ├── main.ts
├── test/
├── .env
├── .env.example
├── .gitignore
├── .prettierrc
├── nest-cli.json
├── package.json
├── tsconfig.json
├── tsconfig.build.json
目录结构介绍
src/
: 包含项目的所有源代码。auth/
: 认证相关模块。common/
: 通用功能和工具。config/
: 配置文件和配置服务。health/
: 健康检查模块。mail/
: 邮件发送模块。notification/
: 通知模块。prisma/
: Prisma ORM 相关文件。seed/
: 数据种子文件。user/
: 用户管理模块。app.module.ts
: 应用程序的根模块。main.ts
: 应用程序的入口文件。
test/
: 包含测试文件。.env
: 环境变量文件。.env.example
: 环境变量示例文件。.gitignore
: Git 忽略文件。.prettierrc
: Prettier 配置文件。nest-cli.json
: NestJS CLI 配置文件。package.json
: 项目依赖和脚本。tsconfig.json
: TypeScript 配置文件。tsconfig.build.json
: TypeScript 构建配置文件。
2. 项目的启动文件介绍
main.ts
main.ts
是 NestJS 应用程序的入口文件。它负责启动应用程序并监听指定的端口。以下是 main.ts
的主要内容:
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { ConfigService } from '@nestjs/config';
import { Logger, ValidationPipe } from '@nestjs/common';
import { setupSwagger } from './swagger';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const configService = app.get(ConfigService);
const PORT = configService.get<number>('PORT');
app.useGlobalPipes(new ValidationPipe());
setupSwagger(app);
await app.listen(PORT);
Logger.log(`Application is running on: ${await app.getUrl()}`);
}
bootstrap();
启动文件介绍
NestFactory.create(AppModule)
: 创建 NestJS 应用程序实例。ConfigService
: 获取配置服务,用于读取环境变量。ValidationPipe
: 全局验证管道,用于数据验证。setupSwagger(app)
: 设置 Swagger 文档。app.listen(PORT)
: 启动应用程序并监听指定端口。
3. 项目的配置文件介绍
.env
.env
文件用于存储环境变量,例如数据库连接字符串、端口号等。以下是一个示例:
PORT=3000
DATABASE_URL=postgresql://user:password@localhost:5432/mydatabase
JWT_SECRET=mysecret
.env.example
.env.example
文件是一个示例文件,用于指导用户如何配置环境变量。它通常包含所有必需的环境变量及其默认值。
nest-cli.json
nest-cli.json
文件是 NestJS CLI 的配置文件,用于配置项目的一些选项,例如路径别名、构建选项等。
{
"collection": "@nestjs/schematics",
"sourceRoot": "src"
}
tsconfig.json
tsconfig.json
文件是 TypeScript
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考