Nest 项目教程
nestNest Pelican Template项目地址:https://gitcode.com/gh_mirrors/nest3/nest
1. 项目的目录结构及介绍
nest/
├── src/
│ ├── app.controller.ts
│ ├── app.module.ts
│ ├── app.service.ts
│ ├── main.ts
│ └── ...
├── test/
│ ├── app.e2e-spec.ts
│ └── jest-e2e.json
├── package.json
├── tsconfig.json
└── ...
src/
:包含项目的源代码文件。app.controller.ts
:应用的控制器。app.module.ts
:应用的根模块。app.service.ts
:应用的服务。main.ts
:项目的启动文件。
test/
:包含项目的测试文件。app.e2e-spec.ts
:端到端测试文件。jest-e2e.json
:Jest 测试配置文件。
package.json
:项目的依赖和脚本配置文件。tsconfig.json
:TypeScript 配置文件。
2. 项目的启动文件介绍
main.ts
是 Nest 项目的启动文件,负责引导应用程序的启动。以下是 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)
:创建 Nest 应用实例。app.listen(3000)
:启动应用并监听 3000 端口。
3. 项目的配置文件介绍
package.json
package.json
文件包含了项目的依赖、脚本和其他元数据。以下是一些关键部分:
{
"name": "nest",
"version": "0.0.1",
"scripts": {
"start": "nest start",
"build": "nest build",
"test": "jest"
},
"dependencies": {
"@nestjs/common": "^8.0.0",
"@nestjs/core": "^8.0.0",
"reflect-metadata": "^0.1.13",
"typescript": "^4.3.5"
},
"devDependencies": {
"@nestjs/testing": "^8.0.0",
"jest": "^27.0.6"
}
}
scripts
:定义了项目的启动、构建和测试命令。dependencies
:项目的运行时依赖。devDependencies
:开发环境下的依赖。
tsconfig.json
tsconfig.json
文件定义了 TypeScript 编译器的配置。以下是一些关键部分:
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"outDir": "./dist",
"strict": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true
},
"include": ["src/**/*"]
}
compilerOptions
:编译选项。module
:指定模块系统。target
:指定编译目标。outDir
:指定输出目录。strict
:启用严格模式。emitDecoratorMetadata
和experimentalDecorators
:启用装饰器元数据和实验性装饰器。
include
:指定包含的文件或目录。
nestNest Pelican Template项目地址:https://gitcode.com/gh_mirrors/nest3/nest
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考