This commit is contained in:
陶林 2022-06-01 16:27:05 +08:00
parent a86d012adc
commit 4bfd893199
12 changed files with 131 additions and 6 deletions

View File

@ -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: [

View File

@ -0,0 +1,4 @@
# 接口限速插件
pass

View File

@ -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<NestExpressApplication>(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.'})
```

14
src/apps/useCli/README.md Normal file
View File

@ -0,0 +1,14 @@
# Cli
Nest CLI是一个命令行界面工具以帮助您初始化、开发和维护 Nest 应用程序。它以多种方式提供帮助,包括搭建项目、以开发模式为其提供服务,以及为生产分发构建和打包应用程序。它体现了最佳实践的架构模式,以构建良好的应用程序。
```shell
npm install -g @nestjs/cli
```
```shell
╰─$ nest -v
8.2.6
```

View File

@ -0,0 +1,2 @@
# Controller

View File

@ -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]);
}
}