<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                ## 基礎搭建 ### MVC配置 ``` // 安裝模板 yarn add ejs --save // main.ts import { join } from 'path'; import { NestFactory } from '@nestjs/core'; // 引入app模塊 import { AppModule } from './app.module'; import { HttpExceptionFilter } from './common/filters/http-exception.filter'; import { ApiParamsValidationPipe } from './common/pipe/api-params-validation.pipe'; import { NestExpressApplication } from "@nestjs/platform-express"; async function bootstrap() { // 工廠方式創建服務器 const app = await NestFactory.create<NestExpressApplication>(AppModule); //設置 視圖層 app.useStaticAssets(join(__dirname, '.', 'public')); app.setBaseViewsDir(join(__dirname, '.', 'views')); app.setViewEngine('ejs'); // 設置錯誤處理 app.useGlobalFilters(new HttpExceptionFilter()); app.useGlobalPipes(new ApiParamsValidationPipe()); await app.listen(3001); // 使用3000端口監聽應用程序 } bootstrap(); // 啟動應用程序 -> localhost:3000 ~~~ ``` ### 錯誤處理 #### 全局錯誤代碼 api-error-code.enum.ts ``` export enum ApiErrorCode { TIMEOUT = -1, // 系統繁忙 SUCCESS = 0, // 成功 USER_ID_INVALID = 10001, // 用戶id無效 USER_NAME_HAVA = 10002, // 用戶已經存在 USER_NAME_NO = 10003, // 用戶無效 USER_NICKNAME_NO = 10004, // 用戶昵稱無效 USER_EMAIL_NO = 10005 // 用戶郵箱無效 } ``` #### 全局過濾錯誤 http-exception.filter.ts ``` import { ArgumentsHost, Catch, ExceptionFilter, HttpException } from '@nestjs/common'; import { ApiException } from '../exceptions/api.exception'; @Catch(HttpException) export class HttpExceptionFilter implements ExceptionFilter { catch(exception, host: ArgumentsHost) { const ctx = host.switchToHttp(); const response = ctx.getResponse(); const request = ctx.getRequest(); const status = exception.getStatus(); if (exception instanceof ApiException) { response .status(status) .json({ errorCode: exception.getErrorCode(), errorMessage: exception.getErrorMessage(), date: new Date().toLocaleDateString(), path: request.url, }); } else { response .status(status) .json({ statusCode: status, date: new Date().toLocaleDateString(), path: request.url, }); } } } ``` #### 全局管道錯誤 api-params-validation.pipe.ts ``` import { ArgumentMetadata, PipeTransform, Injectable, HttpStatus } from '@nestjs/common'; import { ApiException } from '../exceptions/api.exception'; import { plainToClass } from 'class-transformer'; import { validate } from 'class-validator'; @Injectable() export class ApiParamsValidationPipe implements PipeTransform { async transform(value: any, metadata: ArgumentMetadata) { const { metatype } = metadata; // 如果參數不是 類 而是普通的 JavaScript 對象則不進行驗證 if (!metatype || !this.toValidate(metatype)) { return value; } // 通過元數據和對象實例,去構建原有類型 const object = plainToClass(metatype, value); const errors = await validate(object); if (errors.length > 0) { // 獲取到第一個沒有通過驗證的錯誤對象 const error = errors.shift(); const constraints = error.constraints; const contexts = error.contexts; // 將未通過驗證的字段的錯誤信息和狀態碼,以ApiException的形式拋給我們的全局異常過濾器 for (const key in constraints) { throw new ApiException(constraints[key], contexts[key].errorCode, HttpStatus.BAD_REQUEST); } } return value; } private toValidate(metatype): boolean { const types = [String, Boolean, Number, Array, Object]; return !types.find((type) => metatype === type); } } ``` #### 獲取錯誤 api.exception.ts ``` import { HttpException, HttpStatus } from '@nestjs/common'; import { ApiErrorCode } from '../enums/api-error-code.enum'; export class ApiException extends HttpException { private errorMessage: string; private errorCode: ApiErrorCode; constructor(errorMessage: string, errorCode: ApiErrorCode, statusCode: HttpStatus) { super(errorMessage, statusCode); this.errorMessage = errorMessage; this.errorCode = errorCode; } getErrorCode(): ApiErrorCode { return this.errorCode; } getErrorMessage(): string { return this.errorMessage; } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看