NestJS RESTful APIs 示例项目教程
nestjs-rest-sampleNestJS RESTful APIs Sample项目地址:https://gitcode.com/gh_mirrors/ne/nestjs-rest-sample
1. 项目的目录结构及介绍
nestjs-rest-sample/
├── docs/
│ └── guide.md
├── src/
│ ├── app.module.ts
│ ├── main.ts
│ ├── ...
├── test/
├── .eslintrc.js
├── .gitignore
├── .mergify.yml
├── .prettierrc
├── Dockerfile
├── LICENSE
├── README.md
├── codecov.yml
├── commitlint.config.js
├── docker-compose.yml
├── nest-cli.json
├── package-lock.json
├── package.json
├── tsconfig.build.json
└── tsconfig.json
docs/
: 包含项目文档,如 guide.md
。src/
: 包含项目的源代码,如 app.module.ts
和 main.ts
。test/
: 包含项目的测试代码。.eslintrc.js
: ESLint 配置文件。.gitignore
: Git 忽略文件配置。.mergify.yml
: Mergify 配置文件。.prettierrc
: Prettier 配置文件。Dockerfile
: Docker 配置文件。LICENSE
: 项目许可证。README.md
: 项目说明文档。codecov.yml
: Codecov 配置文件。commitlint.config.js
: Commitlint 配置文件。docker-compose.yml
: Docker Compose 配置文件。nest-cli.json
: NestJS CLI 配置文件。package-lock.json
: npm 锁定文件。package.json
: npm 配置文件。tsconfig.build.json
: TypeScript 构建配置文件。tsconfig.json
: TypeScript 配置文件。
2. 项目的启动文件介绍
src/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();
main.ts
是 NestJS 项目的入口文件,负责启动应用程序。- 使用
NestFactory.create
方法创建一个 Nest 应用实例。 - 调用
app.listen
方法在端口 3000 上启动应用。
3. 项目的配置文件介绍
nest-cli.json
{
"collection": "@nestjs/schematics",
"sourceRoot": "src"
}
nest-cli.json
是 NestJS CLI 的配置文件。collection
指定使用的 NestJS 脚手架集合。sourceRoot
指定源代码的根目录。
package.json
{
"name": "nestjs-rest-sample",
"version": "1.0.0",
"description": "NestJS RESTful APIs sample project",
"scripts": {
"start": "nest start",
"build": "nest build",
"test": "jest"
},
"dependencies": {
"@nestjs/common": "^8.0.0",
"@nestjs/core": "^8.0.0",
"@nestjs/platform-express": "^8.0.0",
"reflect-metadata": "^0.1.13",
"typescript": "^4.3.5"
},
"devDependencies": {
"@nestjs/testing": "^8.0.0",
"jest": "^27.0.6"
}
}
package.json
是 npm 配置文件,包含项目的基本信息、脚本命令和依赖项。scripts
定义了常用的脚本命令,如 start
、build
和 test
。dependencies
和 devDependencies
分别列出了项目运行时和开发时的依赖项。
tsconfig.json
{
"compilerOptions": {
"module": "commonjs",
"declaration":
nestjs-rest-sampleNestJS RESTful APIs Sample项目地址:https://gitcode.com/gh_mirrors/ne/nestjs-rest-sample