feat: 更新README说明
parent
ad27bab0c5
commit
876154256c
12
.env
12
.env
|
|
@ -47,3 +47,15 @@ STATIC_DIR = ./content/html
|
||||||
DEFAULT_PAGE = 1
|
DEFAULT_PAGE = 1
|
||||||
# 默认每页数量
|
# 默认每页数量
|
||||||
DEFAULT_SIZE = 10
|
DEFAULT_SIZE = 10
|
||||||
|
|
||||||
|
# ========================================================================================
|
||||||
|
# 邮件配置
|
||||||
|
# ========================================================================================
|
||||||
|
# 主机
|
||||||
|
SMTP_HOST = smtp.163.com
|
||||||
|
# 端口
|
||||||
|
SMTP_PORT = 25
|
||||||
|
# 发送者
|
||||||
|
SMTP_USER =
|
||||||
|
# 授权码
|
||||||
|
SMTP_PASS =
|
||||||
214
README.md
214
README.md
|
|
@ -1,192 +1,28 @@
|
||||||
## 接口文档
|
|
||||||
|
|
||||||
1. 安装依赖
|
一个NestJS起始模板,正在优化中。
|
||||||
|
|
||||||
```
|
## 功能
|
||||||
pnpm i @nestjs/swagger
|
- Swagger接口文档
|
||||||
```
|
- 邮件发送功能(SMTP)
|
||||||
|
- 统一参数校验
|
||||||
|
- 统一异常拦截
|
||||||
|
- 统一响应结构
|
||||||
|
- 统一参数序列化
|
||||||
|
- 数据库功能(支持多配置)
|
||||||
|
- 数据库分页/条件/全部查询
|
||||||
|
- 静态页面
|
||||||
|
- 文件上传
|
||||||
|
- 文件下载
|
||||||
|
- Redis或内存缓存
|
||||||
|
- 日志输出到控制台/文件
|
||||||
|
- 统一配置/环境变量服务(类型安全且带默认值)
|
||||||
|
- 模块/控制器/服务基类
|
||||||
|
- 模板快速生成(业务型模板,非NestJS脚手架的模板)
|
||||||
|
|
||||||
2. 修改`src/main.ts`文件
|
## 模块
|
||||||
|
- 用户模块
|
||||||
```typescript
|
- 登陆模块
|
||||||
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
|
- 角色模块
|
||||||
|
- 权限模块
|
||||||
const config = new DocumentBuilder()
|
- 上传模块
|
||||||
.setTitle('接口文档')
|
- 文章模块
|
||||||
.setVersion('1.0')
|
|
||||||
.setDescription('Openapi 3.0文档')
|
|
||||||
.setExternalDoc('JSON数据', '/openapi-json')
|
|
||||||
.addTag('用户管理', 'user')
|
|
||||||
.build();
|
|
||||||
const document = SwaggerModule.createDocument(app, config);
|
|
||||||
SwaggerModule.setup('openapi', app, document);
|
|
||||||
```
|
|
||||||
|
|
||||||
3. 修改`nest-cli.json`文件
|
|
||||||
|
|
||||||
```json
|
|
||||||
{
|
|
||||||
"compilerOptions": {
|
|
||||||
"plugins": [
|
|
||||||
{
|
|
||||||
"name": "@nestjs/swagger",
|
|
||||||
"options": {
|
|
||||||
"introspectComments": true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
## 权限认证
|
|
||||||
|
|
||||||
1. 依赖
|
|
||||||
```bash
|
|
||||||
pnpm i @nestjs/passport passport passport-local passport-jwt
|
|
||||||
```
|
|
||||||
|
|
||||||
路径:/auth/login - localguard - localService.validate - jwtService.sign
|
|
||||||
|
|
||||||
## 配置文件
|
|
||||||
|
|
||||||
1. 安装依赖
|
|
||||||
|
|
||||||
```shell
|
|
||||||
pnpm i @nestjs/config
|
|
||||||
```
|
|
||||||
|
|
||||||
2. 修改`src/app.module.ts`文件
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
import { ConfigModule } from '@nestjs/config';
|
|
||||||
|
|
||||||
ConfigModule.forRoot({
|
|
||||||
envFilePath: '.env',
|
|
||||||
isGlobal: true
|
|
||||||
}),
|
|
||||||
```
|
|
||||||
|
|
||||||
## 源码分析
|
|
||||||
|
|
||||||
入口
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
export const NestFactory = new NestFactoryStatic();
|
|
||||||
```
|
|
||||||
|
|
||||||
创建 1 个 ApplicationConfig 实例,存放全局拦截器、管道、异常过滤器等。
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
export class ApplicationConfig {
|
|
||||||
private globalPrefix = '';
|
|
||||||
private globalPrefixOptions: GlobalPrefixOptions<ExcludeRouteMetadata> = {};
|
|
||||||
private globalPipes: Array<PipeTransform> = [];
|
|
||||||
private globalFilters: Array<ExceptionFilter> = [];
|
|
||||||
private globalInterceptors: Array<NestInterceptor> = [];
|
|
||||||
private globalGuards: Array<CanActivate> = [];
|
|
||||||
private versioningOptions: VersioningOptions;
|
|
||||||
private readonly globalRequestPipes: InstanceWrapper<PipeTransform>[] = [];
|
|
||||||
private readonly globalRequestFilters: InstanceWrapper<ExceptionFilter>[] = [];
|
|
||||||
private readonly globalRequestInterceptors: InstanceWrapper<NestInterceptor>[] = [];
|
|
||||||
private readonly globalRequestGuards: InstanceWrapper<CanActivate>[] = [];
|
|
||||||
constructor(private ioAdapter: WebSocketAdapter | null = null) {}
|
|
||||||
// ...
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
创建 1 个 NestContainer 实例(即 IOC 容器), 并传入 applicationConfig,存放 modules(模块)等。
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
export class NestContainer {
|
|
||||||
private readonly globalModules = new Set<Module>();
|
|
||||||
private readonly moduleTokenFactory = new ModuleTokenFactory();
|
|
||||||
private readonly moduleCompiler = new ModuleCompiler(this.moduleTokenFactory);
|
|
||||||
private readonly modules = new ModulesContainer();
|
|
||||||
private readonly dynamicModulesMetadata = new Map<string, Partial<DynamicModule>>();
|
|
||||||
private readonly internalProvidersStorage = new InternalProvidersStorage();
|
|
||||||
private readonly _serializedGraph = new SerializedGraph();
|
|
||||||
private internalCoreModule: Module;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
执行 this.initialize 初始化,从根模块开始扫描:
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
private async initialize(
|
|
||||||
module: any,
|
|
||||||
container: NestContainer,
|
|
||||||
graphInspector: GraphInspector,
|
|
||||||
config = new ApplicationConfig(),
|
|
||||||
options: NestApplicationContextOptions = {},
|
|
||||||
httpServer: HttpServer = null,
|
|
||||||
) {
|
|
||||||
const injector = new Injector({ preview: options.preview });
|
|
||||||
const instanceLoader = new InstanceLoader(container,injector,graphInspector,);
|
|
||||||
const metadataScanner = new MetadataScanner();
|
|
||||||
const dependenciesScanner = new DependenciesScanner(container,metadataScanner,graphInspector,config,);
|
|
||||||
const teardown = this.abortOnError === false ? rethrow : undefined;
|
|
||||||
|
|
||||||
container.setHttpAdapter(httpServer);
|
|
||||||
await httpServer?.init();
|
|
||||||
|
|
||||||
try {
|
|
||||||
this.logger.log(MESSAGES.APPLICATION_START);
|
|
||||||
|
|
||||||
await ExceptionsZone.asyncRun(
|
|
||||||
async () => {
|
|
||||||
await dependenciesScanner.scan(module);
|
|
||||||
await instanceLoader.createInstancesOfDependencies();
|
|
||||||
dependenciesScanner.applyApplicationProviders();
|
|
||||||
},
|
|
||||||
teardown,
|
|
||||||
this.autoFlushLogs,
|
|
||||||
);
|
|
||||||
} catch (e) {
|
|
||||||
this.handleInitializationError(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
```
|
|
||||||
NestFactory.create传入1到3个参数:根模块,HTTP适配器,参数, 执行以下过程
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
1. 扫描到到controller的方法时,创建1个执行环境,注入管道、拦截器等内容
|
|
||||||
nestjs/nest/packages/core/router/router-execution-context.ts
|
|
||||||
|
|
||||||
2. 扫描到名为APP_PIPE等provider时,向application注入全局
|
|
||||||
nestjs/nest/packages/core/scanner.ts
|
|
||||||
|
|
||||||
创建1个NestApplication实例,包含对底层的一些封装,例如监听端口、使用全局拦截器等方法。
|
|
||||||
|
|
||||||
```
|
|
||||||
|
|
||||||
创建 1 个对 NestApplication 的代理(target),对所有方法包裹一层异常捕获,核心代码:
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
// file: nestjs/nest/packages/core/router/router-execution-context.ts
|
|
||||||
if (isFunction(receiver[prop])) {
|
|
||||||
return this.createExceptionZone(receiver, prop);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
创建 1 个对 target 和 httpServer 的代理,如果 target 上调用的方法时,则尝试调用 httpServer 上的方法,核心代码:
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
// nestjs/nest/packages/core/nest-factory.ts
|
|
||||||
if (!(prop in receiver) && prop in adapter) {
|
|
||||||
return (...args: unknown[]) => {
|
|
||||||
const result = this.createExceptionZone(adapter, prop)(...args);
|
|
||||||
return mapToProxy(result);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
```
|
|
||||||
## TODO
|
|
||||||
- media video image text audio
|
|
||||||
- name size author path
|
|
||||||
- user
|
|
||||||
- tag color place thing
|
|
||||||
- category
|
|
||||||
|
|
@ -38,6 +38,7 @@
|
||||||
"rxjs": "^7.2.0"
|
"rxjs": "^7.2.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@compodoc/compodoc": "^1.1.21",
|
||||||
"@nestjs/cache-manager": "^2.1.0",
|
"@nestjs/cache-manager": "^2.1.0",
|
||||||
"@nestjs/cli": "^9.0.0",
|
"@nestjs/cli": "^9.0.0",
|
||||||
"@nestjs/config": "^2.3.1",
|
"@nestjs/config": "^2.3.1",
|
||||||
|
|
@ -76,7 +77,6 @@
|
||||||
"mockjs": "^1.1.0",
|
"mockjs": "^1.1.0",
|
||||||
"multer": "1.4.5-lts.1",
|
"multer": "1.4.5-lts.1",
|
||||||
"mysql2": "^3.2.0",
|
"mysql2": "^3.2.0",
|
||||||
"nanoid": "^4.0.1",
|
|
||||||
"nodemailer": "^6.9.4",
|
"nodemailer": "^6.9.4",
|
||||||
"passport": "^0.6.0",
|
"passport": "^0.6.0",
|
||||||
"passport-jwt": "^4.0.1",
|
"passport-jwt": "^4.0.1",
|
||||||
|
|
|
||||||
2294
pnpm-lock.yaml
2294
pnpm-lock.yaml
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue