diff --git a/src/app.module.ts b/src/app.module.ts index 610326a..1d45d2b 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -6,9 +6,9 @@ import { TypeOrmModule } from '@nestjs/typeorm'; import { RateLimiterModule } from 'nestjs-rate-limiter'; import User from './entity/user'; -import { TechModule } from './apps/tech'; -import { CatsModule } from './apps/tech/cats/cats.module'; -import { RateModule } from './apps/rate'; +import { TechModule } from './apps/pluginORM'; +import { CatsModule } from './apps/pluginORM/cats/cats.module'; +import { RateModule } from './apps/pluginRate'; import { UseControllerModule } from './apps/useController'; @Module({ imports: [ diff --git a/src/apps/tech/cats/cats.module.ts b/src/apps/pluginORM/cats/cats.module.ts similarity index 100% rename from src/apps/tech/cats/cats.module.ts rename to src/apps/pluginORM/cats/cats.module.ts diff --git a/src/apps/tech/controller/tech.ts b/src/apps/pluginORM/controller/tech.ts similarity index 100% rename from src/apps/tech/controller/tech.ts rename to src/apps/pluginORM/controller/tech.ts diff --git a/src/apps/tech/entity/tech.ts b/src/apps/pluginORM/entity/tech.ts similarity index 100% rename from src/apps/tech/entity/tech.ts rename to src/apps/pluginORM/entity/tech.ts diff --git a/src/apps/tech/index.ts b/src/apps/pluginORM/index.ts similarity index 100% rename from src/apps/tech/index.ts rename to src/apps/pluginORM/index.ts diff --git a/src/apps/pluginRate/README.md b/src/apps/pluginRate/README.md new file mode 100644 index 0000000..8f98a88 --- /dev/null +++ b/src/apps/pluginRate/README.md @@ -0,0 +1,4 @@ +# 接口限速插件 + +pass + diff --git a/src/apps/rate/controller.ts b/src/apps/pluginRate/controller.ts similarity index 100% rename from src/apps/rate/controller.ts rename to src/apps/pluginRate/controller.ts diff --git a/src/apps/rate/index.ts b/src/apps/pluginRate/index.ts similarity index 100% rename from src/apps/rate/index.ts rename to src/apps/pluginRate/index.ts diff --git a/src/apps/pluginSwagger/README.md b/src/apps/pluginSwagger/README.md new file mode 100644 index 0000000..267c078 --- /dev/null +++ b/src/apps/pluginSwagger/README.md @@ -0,0 +1,66 @@ +# 集成Swagger + +https://docs.nestjs.com/openapi/introduction + +要开始使用,首先安装依赖、 + +$ npm install --save @nestjs/swagger swagger-ui-expressCopy to clipboardErrorCopied + +如果使用fastify,安装fastify-swagger而不是swagger-ui-express: + +$ npm install --save @nestjs/swagger fastify-swagger + +安装完成后,在main.ts文件中定义并初始化SwaggerModule类: + +```js +import { NestFactory } from '@nestjs/core'; +import { NestExpressApplication } from '@nestjs/platform-express'; +import { AppModule } from './app.module'; +import { join } from 'path'; +import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger'; + +async function bootstrap() { + console.log(__dirname); + const app = await NestFactory.create(AppModule); + app.useStaticAssets(join(__dirname, '..', 'public')); + app.setBaseViewsDir(join(__dirname, '..', 'templates')); + app.setViewEngine('hbs'); + app.useStaticAssets(join(__dirname, '..', 'public'), { + prefix: '/static', + }); + + const config = new DocumentBuilder() + .setTitle('Nest.js') + .setDescription('使用Nest.js') + .setVersion('1.0') + .addTag('Use Nest.js') + .build(); + const document = SwaggerModule.createDocument(app, config); + SwaggerModule.setup('api', app, document); + + await app.listen(3000); +} +bootstrap(); +``` + +## 标签 + +```js +@ApiTags('模板使用') + @Get('/temp1') + @Render('index') + temp1() { + return { + message: 'Hello,,Nest', + }; + } +``` + + +## 响应 + + +```js +@ApiResponse({ status: 201, description: 'The record has been successfully created.'}) +@ApiResponse({ status: 403, description: 'Forbidden.'}) +``` \ No newline at end of file diff --git a/src/apps/useCli/README.md b/src/apps/useCli/README.md new file mode 100644 index 0000000..49e3f62 --- /dev/null +++ b/src/apps/useCli/README.md @@ -0,0 +1,14 @@ +# Cli + +Nest CLI是一个命令行界面工具,以帮助您初始化、开发和维护 Nest 应用程序。它以多种方式提供帮助,包括搭建项目、以开发模式为其提供服务,以及为生产分发构建和打包应用程序。它体现了最佳实践的架构模式,以构建良好的应用程序。 + +```shell +npm install -g @nestjs/cli +``` + +```shell +╰─$ nest -v +8.2.6 +``` + + diff --git a/src/apps/useController/README.md b/src/apps/useController/README.md new file mode 100644 index 0000000..b3110cb --- /dev/null +++ b/src/apps/useController/README.md @@ -0,0 +1,2 @@ +# Controller + diff --git a/src/apps/useController/param.ts b/src/apps/useController/param.ts index 5e0f46f..b0fd2d3 100644 --- a/src/apps/useController/param.ts +++ b/src/apps/useController/param.ts @@ -1,12 +1,51 @@ -import { Controller, Get } from '@nestjs/common'; +import { Controller, Get, HttpStatus, Param, Req, Res } from '@nestjs/common'; import { ApiTags } from '@nestjs/swagger'; +import { Response, Request } from 'express'; -@Controller('/useController/param') +@Controller('/useController') @ApiTags('Controller使用') export default class ParamController { // 基本控制器 @Get('/t1') getT1() { - return 'T1'; + return '测试路由'; + } + // Get请求参数 + @Get('/t2') + getT2() { + return '测试路由'; + } + // Post参数 + @Get('/t3') + getT3() { + return '测试路由'; + } + // Request对象 + @Get('/request') + getT4(@Req() request: Request) { + console.log(request); + return { + method: request.method, + url: request.url, + baseUrl: request.baseUrl, + headers: request.headers, + cookies: request.cookies, + statusCode: request.statusCode, + route: request.route, + }; + } + + // Get路径参数 + @Get('/query_param/:name') + getT5(@Param() params) { + console.log(params); + return { + ...params, + }; + } + /// 自定义Response + @Get('/response') + getT6(@Res() res: Response) { + res.status(HttpStatus.CREATED).send([1, 2, 3]); } }