Appearance
安装
bash
npm i --save @nestjs/core @nestjs/common rxjs reflect-metadata @nestjs/platform-express
npm install -g ts-node
1
2
2
包名 | 介绍 |
---|---|
@nestjs/core | NestJS 框架的核心模块,提供构建、启动和管理 NestJS 应用程序的基础设施。 |
@nestjs/common | 包含构建 NestJS 应用的基础设施和常用装饰器、工具类、接口等,用于定义控制器、服务、中间件、守卫、拦截器、管道、异常过滤器等。 |
rxjs | 用于构建异步和事件驱动程序的库,基于可观察序列的概念,提供强大的功能来处理异步数据流。 |
reflect-metadata | 在 JavaScript 和 TypeScript 中实现元编程的库,通过提供元数据反射 API,允许在运行时检查和操作对象的元数据。 |
@nestjs/platform-express | NestJS 的平台适配器,用于将 NestJS 应用与 Express.js 集成,提供 Express.js 的中间件、路由等功能,并享受 NestJS 的模块化、依赖注入等高级特性。 |
ts-node | 是一个用于直接执行 TypeScript 代码的 Node.js 实现,它允许开发者在不预先编译的情况下运行 TypeScript 文件 |
src\main.ts
src\main.ts
typescript
// 从 @nestjs/core 模块中导入 NestFactory,用于创建 Nest 应用实例
import { NestFactory } from '@nestjs/core';
// 导入应用的根模块 AppModule
import { AppModule } from './app.module';
// 定义一个异步函数 bootstrap,用于启动应用
async function bootstrap() {
// 使用 NestFactory.create 方法创建一个 Nest 应用实例,并传入根模块 AppModule
const app = await NestFactory.create(AppModule);
// 让应用监听 3000 端口
await app.listen(3000);
}
// 调用 bootstrap 函数,启动应用
bootstrap();
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
NestFactory
是 NestJS 框架中用于创建 Nest 应用实例的核心类。它提供了一组静态方法,用于引导和启动应用程序。
NestFactory.create
创建一个 Nest 应用实例,默认使用 Express 作为底层 HTTP 服务器。
app.module.ts
src\app.module.ts
typescript
// 从 '@nestjs/common' 模块中导入 Module 装饰器
import { Module } from '@nestjs/common';
// 从当前目录导入 AppController 控制器
import { AppController } from './app.controller';
// 使用 @Module 装饰器定义一个模块
@Module({
// 在 controllers 属性中指定当前模块包含的控制器
controllers: [AppController],
})
// 定义并导出 AppModule 模块
export class AppModule {}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
@Module
是 NestJS 框架中的一个装饰器,用于定义模块。模块是组织代码的基本单元,它们将相关的组件(如控制器、服务、提供者等)组合在一起。NestJS 的模块系统受到了 Angular 的启发,旨在促进代码的模块化和可维护性。
app.controller.ts
src\app.controller.ts
typescript
// 导入 Controller 和 Get 装饰器
import { Controller, Get } from '@nestjs/common';
// 使用 @Controller 装饰器标记类为控制器
@Controller()
export class AppController {
// 构造函数,目前没有任何参数和逻辑
constructor() {}
// 使用 @Get 装饰器标记方法为处理 GET 请求的路由
@Get()
// 定义 getHello 方法,返回类型为字符串
getHello(): string {
// 返回字符串 'hello'
return 'hello';
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
@Controller
是 NestJS 框架中的一个装饰器,用于定义控制器。控制器是处理传入 HTTP 请求的核心组件。每个控制器负责处理特定的请求路径和相应的 HTTP 方法。控制器使用路由装饰器(如 @Get
、@Post
等)来定义路由和请求处理方法。
@Get
是 NestJS 框架中的一个装饰器,用于将控制器方法映射到 HTTP GET 请求。这个装饰器是由 @nestjs/common
模块提供的。通过使用 @Get
装饰器,可以指定该方法处理特定路径上的 GET 请求。
package.json
package.json
json
{
"name": "2.first-step",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "ts-node src/main.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@nestjs/common": "^10.3.9",
"@nestjs/core": "^10.3.9",
"@nestjs/platform-express": "^10.3.9",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1"
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
tsconfig.json
tsconfig.json
json
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"target": "ES2021",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": false,
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false,
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
选项名 | 选项介绍 |
---|---|
module | 指定生成的模块代码的模块系统,commonjs 是 Node.js 的模块系统。 |
declaration | 生成 .d.ts 声明文件。 |
removeComments | 删除编译后的注释。 |
emitDecoratorMetadata | 为装饰器生成元数据。 |
experimentalDecorators | 启用实验性的装饰器特性。 |
esModuleInterop | 允许从没有默认导出的模块中默认导入。这对于兼容性模块非常有用。 |
target | 指定 ECMAScript 目标版本,ES2021 是一种现代的 JavaScript 版本。 |
sourceMap | 生成对应的 .map 文件。 |
outDir | 指定编译输出目录为 ./dist 。 |
baseUrl | 设置解析非相对模块名的基准目录为 ./ 。 |
incremental | 启用增量编译,提升编译速度。 |
skipLibCheck | 跳过对所有声明文件的类型检查。 |
strictNullChecks | 启用严格的空值检查。 |
noImplicitAny | 禁止隐式 any 类型。 |
strictBindCallApply | 启用严格的 bind 、call 和 apply 方法检查。 |
forceConsistentCasingInFileNames | 强制文件名大小写一致。 |
noFallthroughCasesInSwitch | 禁止 switch 语句中的 case 语句贯穿(fall through)。 |