金狮镖局 Design By www.egabc.com
一般开发过程中不不会根据httpcode来判断接口请求成功与失败的,而是会根据请求返回的数据,里面加上code字段
一、返回的数据格式对比
1、直接返回的数据格式
{ "id": 1, "uuid": "cbbe7abc-b95e-48a0-8d24-b1ac93c45328", "name": "哈士奇1", "age": 12, "color": null, "createAt": "2019-07-25T09:13:30.000Z", "updateAt": "2019-07-25T09:13:30.000Z" }
2、我们自己包装后的返回数据
{ code: 0, message: "请求成功", data: { "id": 1, "uuid": "cbbe7abc-b95e-48a0-8d24-b1ac93c45328", "name": "哈士奇1", "age": 12, "color": null, "createAt": "2019-07-25T09:13:30.000Z", "updateAt": "2019-07-25T09:13:30.000Z" } }
二、拦截全部的错误请求,统一返回格式
1、使用命令创建一个过滤器
nest g f filters/httpException
2、过滤器的代码
import { ArgumentsHost, Catch, ExceptionFilter, HttpException, HttpStatus, Logger, } from '@nestjs/common'; @Catch(HttpException) export class HttpExceptionFilter implements ExceptionFilter { catch(exception: HttpException, host: ArgumentsHost) { const ctx = host.switchToHttp(); const response = ctx.getResponse(); const request = ctx.getRequest(); const message = exception.message.message; Logger.log('错误提示', message); const errorResponse = { data: { error: message, }, // 获取全部的错误信息 message: '请求失败', code: 1, // 自定义code url: request.originalUrl, // 错误的url地址 }; const status = exception instanceof HttpException "htmlcode">... import { HttpExceptionFilter } from './filters/http-exception.filter'; async function bootstrap() { ... // 全局注册错误的过滤器 app.useGlobalFilters(new HttpExceptionFilter()); } bootstrap();4、测试,返回的错误信息
{ "statusCode": 400, "error": "Bad Request", "data": { "message": [ { "age": "必须的整数" } ] }, "message": '请求失败', "code": 1, "url": "/api/v1/cat" }三、统一请求成功的返回数据
1、创建一个拦截器src/interceptor/transform.interceptor.ts
2、拦截器的代码
import { Injectable, NestInterceptor, CallHandler, ExecutionContext, } from '@nestjs/common'; import { map } from 'rxjs/operators'; import { Observable } from 'rxjs'; interface Response<T> { data: T; } @Injectable() export class TransformInterceptor<T> implements NestInterceptor<T, Response<T { intercept( context: ExecutionContext, next: CallHandler<T>, ): Observable<Response<T { return next.handle().pipe( map(data => { return { data, code: 0, message: '请求成功', }; }), ); } }3、全局注册
... import { TransformInterceptor } from './interceptor/transform.interceptor'; async function bootstrap() { ... // 全局注册拦截器 app.useGlobalInterceptors(new TransformInterceptor()); ... } bootstrap();4、测试返回数据
{ "data": { "id": 1, "uuid": "cbbe7abc-b95e-48a0-8d24-b1ac93c45328", "name": "哈士奇1", "age": 12, "color": null, "createAt": "2019-07-25T09:13:30.000Z", "updateAt": "2019-07-25T09:13:30.000Z" }, "code": 0, "message": "请求成功" }
金狮镖局 Design By www.egabc.com
金狮镖局
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金狮镖局 Design By www.egabc.com
暂无nestjs返回给前端数据格式的封装实现的评论...
更新日志
2024年09月23日
2024年09月23日
- 华少翌.2005-华少翌【中唱艺能】【WAV+CUE】
- 群星《十二女声Ⅱ》[WAV/CUE/分轨][527MB]
- 大自然音乐系列《漫步在山裡》[WAV/CUE/分轨][502.8MB]
- 大自然音乐系列《星光夜语》[WAV/CUE/分轨][453.3MB]
- 史依弘《史依弘交响乐京剧演唱专辑HQ》头版限量编号[WAV+CUE]
- 曼丽2020《女人三十风继续吹》HQCD[WAV+CUE]
- 曼丽2021《女人三十2雾之恋》HQCD[WAV+CUE]
- 隔壁老樊.2019-我曾【好听音乐】【FLAC分轨】
- 高林生.1993-牵挂你的人是我【白天鹅】【WAV+CUE】
- 洪卓立.2012-找个懂我的女孩(国专)【英皇娱乐】【WAV+CUE】
- 曼丽2022《女人三十3晚秋》HQCD[WAV+CUE]
- 曼丽2018《女人三十4冬天》HQCD[WAV+CUE]
- 柏菲-《好歌30年壹》限量开盘母带ORMCD[低速原抓WAV+CUE]
- 大自然音乐系列《水舞》[WAV/CUE/分轨][424.3MB]
- 大自然音乐系列《星光夜语》[WAV/CUE/分轨][453.3MB]