nestjs返回給前端數(shù)據(jù)格式的封裝實(shí)現(xiàn)
一般開發(fā)過程中不不會(huì)根據(jù)httpcode來判斷接口請(qǐng)求成功與失敗的,而是會(huì)根據(jù)請(qǐng)求返回的數(shù)據(jù),里面加上code字段
一、返回的數(shù)據(jù)格式對(duì)比
1、直接返回的數(shù)據(jù)格式
{ "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、我們自己包裝后的返回?cái)?shù)據(jù)
{ code: 0, message: "請(qǐng)求成功", 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" } }
二、攔截全部的錯(cuò)誤請(qǐng)求,統(tǒng)一返回格式
1、使用命令創(chuàng)建一個(gè)過濾器
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('錯(cuò)誤提示', message); const errorResponse = { data: { error: message, }, // 獲取全部的錯(cuò)誤信息 message: '請(qǐng)求失敗', code: 1, // 自定義code url: request.originalUrl, // 錯(cuò)誤的url地址 }; const status = exception instanceof HttpException ? exception.getStatus() : HttpStatus.INTERNAL_SERVER_ERROR; // 設(shè)置返回的狀態(tài)碼、請(qǐng)求頭、發(fā)送錯(cuò)誤信息 response.status(status); response.header('Content-Type', 'application/json; charset=utf-8'); response.send(errorResponse); } }
3、在main.ts中全局注冊(cè)
... import { HttpExceptionFilter } from './filters/http-exception.filter'; async function bootstrap() { ... // 全局注冊(cè)錯(cuò)誤的過濾器 app.useGlobalFilters(new HttpExceptionFilter()); } bootstrap();
4、測(cè)試,返回的錯(cuò)誤信息
{ "statusCode": 400, "error": "Bad Request", "data": { "message": [ { "age": "必須的整數(shù)" } ] }, "message": '請(qǐng)求失敗', "code": 1, "url": "/api/v1/cat" }
三、統(tǒng)一請(qǐng)求成功的返回?cái)?shù)據(jù)
1、創(chuàng)建一個(gè)攔截器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: '請(qǐng)求成功', }; }), ); } }
3、全局注冊(cè)
... import { TransformInterceptor } from './interceptor/transform.interceptor'; async function bootstrap() { ... // 全局注冊(cè)攔截器 app.useGlobalInterceptors(new TransformInterceptor()); ... } bootstrap();
4、測(cè)試返回?cái)?shù)據(jù)
{ "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": "請(qǐng)求成功" }
到此這篇關(guān)于nestjs返回給前端數(shù)據(jù)格式的封裝實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)nestjs返回給前端數(shù)據(jù)格式內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
javascript對(duì)talbe進(jìn)行動(dòng)態(tài)添加、刪除、驗(yàn)證實(shí)現(xiàn)代碼
javascript對(duì)talbe進(jìn)行動(dòng)態(tài)添加、刪除、驗(yàn)證實(shí)現(xiàn)代碼,需要的朋友可以參考下2012-03-03IE6中鏈接A的href為javascript協(xié)議時(shí)不在當(dāng)前頁面跳轉(zhuǎn)
IE6中當(dāng)鏈接A的href為javascript協(xié)議時(shí)不能在當(dāng)前頁面跳轉(zhuǎn),本例給出有效的解決方法,大家不妨參考下2014-06-06原生JS控制多個(gè)滾動(dòng)條同步跟隨滾動(dòng)效果
本文要探討的是,當(dāng)這兩個(gè)容器元素的內(nèi)容都超出了容器高度,即都出現(xiàn)了滾動(dòng)框的時(shí)候,如何在其中一個(gè)容器元素滾動(dòng)時(shí),讓另外一個(gè)元素也隨之滾動(dòng)2017-12-12bootstrap校驗(yàn)laydate起止日期聯(lián)動(dòng)失效問題及解決方法
這篇文章主要介紹了bootstrap校驗(yàn)laydate起止日期聯(lián)動(dòng)失效問題,項(xiàng)目中用到bootstrapValidator,以及l(fā)aydate(by:賢心,插件效果美觀),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01理解 JavaScript Scoping & Hoisting(二)
這篇文章主要介紹了理解 JavaScript Scoping & Hoisting,盡管對(duì)于有經(jīng)驗(yàn)的程序員來說這只是小菜一碟,不過我還是順著初學(xué)者常見的思路做一番描述2015-11-11你不知道的 TypeScript 高級(jí)類型(小結(jié))
這篇文章主要介紹了你不知道的 TypeScript 高級(jí)類型(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08js 正則驗(yàn)證密碼強(qiáng)度(包含數(shù)字+特殊字符+英文字母大小寫)
密碼驗(yàn)證是常見的網(wǎng)站注冊(cè)方法,本文主要介紹了js 正則驗(yàn)證密碼強(qiáng)度(包含數(shù)字+特殊字符+英文字母大小寫),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01