NestJS Boilerplate 项目教程

NestJS Boilerplate 项目教程

nestjs-boilerplateNestJS boilerplate. Auth, TypeORM, Postgres, Mailing, I18N, Docker.项目地址: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

nestjs-boilerplateNestJS boilerplate. Auth, TypeORM, Postgres, Mailing, I18N, Docker.项目地址:https://gitcode.com/gh_mirrors/ne/nestjs-boilerplate

  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郦琳凤Joyce

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值