{"id":10008191,"date":"2023-08-29T23:01:10","date_gmt":"2023-08-29T15:01:10","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008191"},"modified":"2023-08-29T23:01:10","modified_gmt":"2023-08-29T15:01:10","slug":"%e3%80%90nestjs%e7%b3%bb%e5%88%97%e3%80%91%e8%bf%9e%e6%8e%a5%e6%95%b0%e6%8d%ae%e5%ba%93%e5%8f%8a%e4%bc%98%e9%9b%85%e5%9c%b0%e5%a4%84%e7%90%86%e5%93%8d%e5%ba%94","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008191","title":{"rendered":"\u3010NestJS\u7cfb\u5217\u3011\u8fde\u63a5\u6570\u636e\u5e93\u53ca\u4f18\u96c5\u5730\u5904\u7406\u54cd\u5e94"},"content":{"rendered":"<h2>\u524d\u8a00<\/h2>\n<p>Node\u4f5c\u4e3a\u4e00\u95e8\u540e\u7aef\u8bed\u8a00\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u8fde\u63a5\u6570\u636e\u5e93\uff0c\u4e3a\u524d\u7aef\u63d0\u4f9bCURD\u63a5\u53e3<\/p>\n<p>\u6211\u4eec\u4ee5<code>mysql<\/code>\u4e3a\u4f8b\uff0c\u81ea\u884c\u5b89\u88c5<code>mysql<\/code><\/p>\n<h2>TypeORM<\/h2>\n<blockquote><p>TypeORM \u662f\u4e00\u4e2a<a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=7K80DiAOU7xLrZaojtvWNg%3D%3D.EnoFSYawZJibQvLz71uMy1bTOJy7gwMaNhD0sKSHZMS2vj5iylQ613PrSMfw50wrON8Ps7ybRKBzkQ1UqF8h%2Bg%3D%3D\" rel=\"noopener noreferrer\">ORM<\/a>\u6846\u67b6\uff0c\u5b83\u53ef\u4ee5\u8fd0\u884c\u5728 NodeJS\u3001Browser\u3001Cordova\u3001PhoneGap\u3001Ionic\u3001React Native\u3001Expo \u548c Electron \u5e73\u53f0\u4e0a\uff0c\u53ef\u4ee5\u4e0e TypeScript \u548c JavaScript\u4e00\u8d77\u4f7f\u7528\u3002 \u5b83\u7684\u76ee\u6807\u662f\u59cb\u7ec8\u652f\u6301\u6700\u65b0\u7684 JavaScript \u7279\u6027\u5e76\u63d0\u4f9b\u989d\u5916\u7684\u7279\u6027\u4ee5\u5e2e\u52a9\u4f60\u5f00\u53d1\u4efb\u4f55\u4f7f\u7528\u6570\u636e\u5e93\u7684\uff08\u4e0d\u7ba1\u662f\u53ea\u6709\u51e0\u5f20\u8868\u7684\u5c0f\u578b\u5e94\u7528\u8fd8\u662f\u62e5\u6709\u591a\u6570\u636e\u5e93\u7684\u5927\u578b\u4f01\u4e1a\u5e94\u7528\uff09\u5e94\u7528\u7a0b\u5e8f\u3002<\/p><\/blockquote>\n<p><code>TypeORM<\/code>\u4f5c\u4e3a<code>TypeScript<\/code>\u4e2d\u6700\u6210\u719f\u7684\u5bf9\u8c61\u5173\u7cfb\u6620\u5c04\u5668\uff0c\u53ef\u4ee5\u5f88\u597d\u7684\u4e0e<code>Nest<\/code>\u6846\u67b6\u96c6\u6210\u4f7f\u7528\u3002<\/p>\n<h3>\u5b89\u88c5\u4f9d\u8d56<\/h3>\n<pre><code class=\"shell\">npm install --save @nestjs\/typeorm typeorm mysql2<\/code><\/pre>\n<h3>\u65b0\u5efa\u6570\u636e\u5e93<\/h3>\n<pre><code class=\"sql\">CREATE DATABASE nanjiu\n    DEFAULT CHARACTER SET = 'utf8mb4';<\/code><\/pre>\n<p>\u65b0\u5efa\u4e00\u4e2a<code>nanjiu<\/code>\u6570\u636e\u5e93<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044162280\" alt=\"7-1.png\" title=\"7-1.png\"><\/p>\n<h3>\u8fde\u63a5\u6570\u636e\u5e93<\/h3>\n<p>\u6570\u636e\u5e93\u5efa\u597d\u4e4b\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u4f7f\u7528<code>typeorm<\/code>\u6765\u8fde\u63a5\u6570\u636e\u5e93\u5e76\u5efa\u7acb\u6620\u5c04\u5173\u7cfb\u4e86<\/p>\n<pre><code class=\"js\">\/\/ dbConfig.ts\n\/\/ \u6570\u636e\u5e93\u914d\u7f6e\nexport function dbConfig()  {\n    return {\n        type: 'mysql', \/\/ \u6570\u636e\u5e93\u7c7b\u578b\n        host: '127.0.0.1', \/\/ \u6570\u636e\u5e93\u5730\u5740\n        port: 3306, \/\/ \u7aef\u53e3\n        username: 'root', \/\/ \u7528\u6237\u540d\n        password: '123456', \/\/ \u5bc6\u7801\n        database: 'nanjiu', \/\/ \u6570\u636e\u5e93\u540d\n        entities: [__dirname + '\/..\/**\/*.entity{.ts,.js}'], \/\/ \u5b9e\u4f53\u7c7b\n        synchronize: true, \/\/ \u81ea\u52a8\u521b\u5efa\u8868\n        autoLoadEntities: true, \/\/ \u81ea\u52a8\u52a0\u8f7d\u5b9e\u4f53\u7c7b\n    } as DbConfig\n}<\/code><\/pre>\n<p>\u9700\u8981\u5728<code>app.module.ts<\/code>\u4e2d\u8fdb\u884c\u6ce8\u518c<\/p>\n<pre><code class=\"js\">@Module({\n  imports: [\n    NanjiuModule, UserModule, InfoModule, \n    TypeOrmModule.forRoot(dbConfig() as any)\n  ],\n  controllers: [AppController],\n  providers: [AppService],\n})<\/code><\/pre>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044162281\" alt=\"7-2.png\" title=\"7-2.png\" loading=\"lazy\"><\/p>\n<h3>\u5b9a\u4e49\u5b9e\u4f53<\/h3>\n<blockquote><p>\u5b9e\u4f53\u662f\u4e00\u4e2a\u6620\u5c04\u5230\u6570\u636e\u5e93\u8868\u7684\u7c7b\uff0c\u4f7f\u7528<code>@Entity<\/code>\u88c5\u9970\u5668\u6765\u5b9a\u4e49<\/p><\/blockquote>\n<pre><code class=\"js\">\/\/ user.entry.ts\nimport { Column, Entity, PrimaryGeneratedColumn } from &quot;typeorm&quot;;\n\n@Entity('user')  \/\/ \u8868\u540d\nexport class User {\n\n    @PrimaryGeneratedColumn() \/\/ \u81ea\u589e\u4e3b\u952e\n    id: number;\n\n    @Column() \/\/ \u5b57\u6bb5\n    name: string;\n}<\/code><\/pre>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044162282\" alt=\"7-3.png\" title=\"7-3.png\" loading=\"lazy\"><\/p>\n<p>\u57fa\u672c\u5b9e\u4f53\u7531\u5217\u548c\u5173\u7cfb\u7ec4\u6210\uff0c\u6bcf\u4e2a\u5b9e\u4f53\u5fc5\u987b\u6709\u4e00\u4e2a\u4e3b\u5217\u3002<\/p>\n<p>\u6bcf\u4e2a\u5b9e\u4f53\u90fd\u5fc5\u987b\u5728\u8fde\u63a5\u914d\u7f6e\u4e2d\u6ce8\u518c\uff1a<\/p>\n<pre><code class=\"js\">entities: [__dirname + '\/..\/**\/*.entity{.ts,.js}'], \/\/ \u5b9e\u4f53\u7c7b<\/code><\/pre>\n<h3>\u5173\u8054\u5b9e\u4f53<\/h3>\n<p>\u5b9e\u4f53\u5b9a\u4e49\u540e\u9700\u8981\u5728<code>module<\/code>\u4e2d\u5bfc\u5165\u5e76\u5173\u8054<\/p>\n<pre><code class=\"js\">@Module({\n  imports: [TypeOrmModule.forFeature([User])],\n  controllers: [UserController],\n  providers: [UserService]\n})<\/code><\/pre>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044162283\" alt=\"7-4.png\" title=\"7-4.png\" loading=\"lazy\"><\/p>\n<p>\u5f53\u4f60\u505a\u5b8c\u8fd9\u4e00\u6b65\u4e4b\u540e\u4f60\u4f1a\u53d1\u73b0\u6570\u636e\u5e93\u91cc\u5df2\u7ecf\u6839\u636e\u4f60\u521a\u521a\u5b9a\u4e49\u7684\u5b9e\u4f53\u5efa\u597d\u4e86\u8868<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044162284\" alt=\"7-5.png\" title=\"7-5.png\" loading=\"lazy\"><\/p>\n<p>\u8fd9\u662f\u56e0\u4e3a\u521a\u521a\u6570\u636e\u5e93\u914d\u7f6e\u90a3\u91cc\u5f00\u542f\u4e86<code>synchronize: true<\/code> \u81ea\u52a8\u521b\u5efa\u8868<\/p>\n<h3>CURD\u63a5\u53e3<\/h3>\n<p>\u6570\u636e\u5e93\u51c6\u5907\u51c6\u5907\u5de5\u4f5c\u5b8c\u6210\u540e\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u6765\u5199\u63a5\u53e3\u4e86<\/p>\n<p><strong>\u5728<code>controller<\/code>\u63a7\u5236\u5668\u4e2d\u5b9a\u4e49\u63a5\u53e3<code>path<\/code><\/strong><\/p>\n<pre><code class=\"js\">\/\/ user.controller.ts\nimport { CreateUserDto } from '.\/dto\/create-user.dto';\nexport class UserController {\n  constructor(\n    private readonly userService: UserService,\n    ) {}\n\n  @Post('addUser')\n  create(@Body() createUserDto: CreateUserDto) {\n    \/\/ \u6dfb\u52a0\u7528\u6237\n    return this.userService.add(createUserDto);\n  }\n}<\/code><\/pre>\n<p><strong>\u65b0\u5efaDTO\u6570\u636e\u9a8c\u8bc1\u5668<\/strong><\/p>\n<pre><code class=\"js\">import { Injectable } from &quot;@nestjs\/common&quot;;\nimport { IsNotEmpty, IsString } from &quot;class-validator&quot;; \/\/ \u5f15\u5165\u9a8c\u8bc1\u5668\n@Injectable() \nexport class CreateUserDto {\n    @IsString({ message: '\u7528\u6237\u540d\u5fc5\u987b\u662f\u5b57\u7b26\u4e32'}) \/\/ \u9a8c\u8bc1\u662f\u5426\u662f\u5b57\u7b26\u4e32\n    @IsNotEmpty({ message: '\u7528\u6237\u540d\u4e0d\u80fd\u4e3a\u7a7a'}) \/\/ \u9a8c\u8bc1\u662f\u5426\u4e3a\u7a7a\n    name: string; \/\/ \u7528\u6237\u540d\n}<\/code><\/pre>\n<p><code>dto<\/code>\u4e00\u822c\u7528\u6765\u505a\u53c2\u6570\u9a8c\u8bc1<\/p>\n<p><strong>\u6ce8\u518c\u5168\u5c40DTO\u9a8c\u8bc1\u7ba1\u9053<\/strong><\/p>\n<pre><code class=\"js\">\/\/ main.ts\nimport { ValidationPipe } from '@nestjs\/common';\n\napp.useGlobalPipes(new ValidationPipe()) \/\/ \u5168\u5c40\u9a8c\u8bc1\u7ba1\u9053<\/code><\/pre>\n<p><strong>service\u903b\u8f91\u5904\u7406\uff0c\u5165\u5e93\u64cd\u4f5c<\/strong><\/p>\n<pre><code class=\"js\">\/\/ user.service.ts\nimport { Injectable } from '@nestjs\/common';\nimport { CreateUserDto } from '.\/dto\/create-user.dto';\nimport { User } from '.\/entities\/user.entity';\nimport { InjectRepository } from '@nestjs\/typeorm';\nimport { Repository } from 'typeorm';\n\n@Injectable()\nexport class UserService {\n  constructor(\n    \/\/ \u4f7f\u7528 @InjectRepository(User) \u6ce8\u5165\u5b9e\u6570\u636e\u5e93\u5b9e\u4f53\n    @InjectRepository(User)\n    private readonly userRepository: Repository&lt;User&gt;\n  ) {}\n\n async add(createUserDto: CreateUserDto) {\n    \/\/ \u6dfb\u52a0\u7528\u6237\uff0c\u66f4\u591a\u64cd\u4f5c\u53c2\u8003 TypeORM \u6587\u6863\n    const res = await this.userRepository.save(createUserDto);\n    return res\n  }\n}<\/code><\/pre>\n<p><strong>\u8c03\u7528\u63a5\u53e3<\/strong><\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044162285\" alt=\"7-6.png\" title=\"7-6.png\" loading=\"lazy\"><\/p>\n<p><strong>\u67e5\u770b\u6570\u636e\u5e93<\/strong><\/p>\n<p>\u8c03\u7528\u5b8c\u63a5\u53e3\uff0c\u6b64\u65f6\u6570\u636e\u5e93\u4e2d\u4f1a\u65b0\u589e\u4e00\u6761\u6570\u636e<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044162286\" alt=\"7-7.png\" title=\"7-7.png\" loading=\"lazy\"><\/p>\n<h2>\u54cd\u5e94\u7ed3\u679c\u5904\u7406<\/h2>\n<p>\u4ece\u4e0a\u9762\u7684\u54cd\u5e94\u7ed3\u679c\u6765\u770b\u5e76\u4e0d\u89c4\u8303\uff0c\u53ea\u662f\u7b80\u5355\u7684\u8fd4\u56de\u4e86\u6570\u636e\u5e93\u67e5\u8be2\u7ed3\u679c\uff0c\u5e76\u4e14\u5f53\u7cfb\u7edf\u53d1\u751f\u5f02\u5e38\u9519\u8bef\u65f6\uff0c\u5982\u679c\u6211\u4eec\u6ca1\u6709\u624b\u52a8\u5904\u7406\u5f02\u5e38\uff0c\u6240\u6709\u7684\u5f02\u5e38\u90fd\u4f1a\u8fdb\u5165\u5230<code>nest<\/code>\u5185\u7f6e\u7684\u5f02\u5e38\u5904\u7406\u5c42\uff0c\u5b83\u8fd4\u56de\u7684\u4fe1\u606f\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"js\">{\n  &quot;statusCode&quot;: 500,\n  &quot;message&quot;: &quot;Internal server error&quot;\n}<\/code><\/pre>\n<p>\u6bd4\u5982\u6211\u4eec\u5f80<code>user<\/code>\u5e93\u4e2d\u63d2\u5165\u76f8\u540c\u7684<code>name<\/code>\uff0c\u4f46<code>name<\/code>\u8bbe\u7f6e\u4e86\u552f\u4e00\u6027\uff0c\u6240\u4ee5\u8fd9\u65f6\u4f1a\u629b\u51fa\u9519\u8bef\uff0c\u5982\u679c\u6211\u4eec\u4e0d\u5904\u7406\u8fd4\u56de\u7ed9\u524d\u7aef\u5c31\u662f\u4e0a\u9762\u90a3\u79cd\u4fe1\u606f\uff0c\u8fd9\u6837\u524d\u7aef\u540c\u5b66\u770b\u5230\u5c31\u4f1a\u5f88\u8499\uff0c\u6839\u672c\u4e0d\u77e5\u9053\u4e3a\u5565\u62a5\u9519<\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044162287\" alt=\"7-8.png\" title=\"7-8.png\" loading=\"lazy\"><\/p>\n<p>\u6240\u4ee5\u6211\u4eec\u8981\u505a\u7684\u5c31\u662f\u5c06\u54cd\u5e94\u683c\u5f0f\u5316\u5904\u7406<\/p>\n<blockquote><p>\u5728nest\u4e2d\uff0c\u4e00\u822c\u662f\u5728<strong>service<\/strong>\u4e2d\u5904\u7406\u5f02\u5e38\uff0c\u5982\u679c\u6709\u5f02\u5e38\uff0c\u76f4\u63a5\u629b\u51fa\u9519\u8bef\uff0c\u7531<strong>\u8fc7\u6ee4\u5668<\/strong>\u6355\u83b7\uff0c\u7edf\u4e00\u683c\u5f0f\u8fd4\u56de\uff0c\u5982\u679c\u6210\u529f\uff0cservice\u628a\u7ed3\u679c\u8fd4\u56de\uff0ccontroller\u76f4\u63a5return\u7ed3\u679c\u5373\u53ef\uff0c\u7531<strong>\u62e6\u622a\u5668<\/strong>\u6355\u83b7\uff0c\u7edf\u4e00\u683c\u5f0f\u8fd4\u56de<br \/>\u5931\u8d25\uff1a\u8fc7\u6ee4\u5668\u7edf\u4e00\u5904\u7406<br \/>\u6210\u529f\uff1a\u62e6\u622a\u5668\u7edf\u4e00\u5904\u7406<\/p><\/blockquote>\n<h3>\u5f02\u5e38\u62e6\u622a\u5668<\/h3>\n<p>\u4e3a\u4e86\u66f4\u52a0\u4f18\u96c5\u5730\u5904\u7406\u5f02\u5e38\uff0c\u6211\u4eec\u53ef\u4ee5\u521b\u5efa\u4e00\u4e2a\u5f02\u5e38\u8fc7\u6ee4\u5668\uff0c\u5b83\u4e3b\u8981\u7528\u6765\u6355\u83b7\u4f5c\u4e3a<code>HttpException<\/code>\u7c7b\u5b9e\u4f8b\u7684\u5f02\u5e38\u3002<\/p>\n<p><strong>\u5f02\u5e38\u629b\u51fa\u5c01\u88c5\uff1a<\/strong><\/p>\n<pre><code class=\"js\">\/\/ httpStatus.service.ts\nimport { Injectable, HttpException, HttpStatus, NestInterceptor } from '@nestjs\/common'\n\n@Injectable()\nexport class HttpStatusError {\n    static fail(error, status = HttpStatus.BAD_REQUEST) {\n        throw new HttpException({statusCode: status, message: '\u8bf7\u6c42\u5931\u8d25', error}, status)\n    }\n}<\/code><\/pre>\n<p><strong>\u629b\u51fa\u5f02\u5e38\uff1a<\/strong><\/p>\n<pre><code class=\"js\">\/\/ group.service.ts\n\/\/ ...\nimport { HttpStatusError } from '..\/utils\/httpStatus.service'\n\n@Injectable()\nexport class GroupService {\n  constructor(\n    @InjectRepository(Group)\n    private groupRepository: Repository&lt;Group&gt;,\n    @InjectRepository(Template)\n    private templateRepository: Repository&lt;Template&gt;,\n  ) {}\n  \/\/ todo: \u6dfb\u52a0\u5206\u7ec4\n  async create(createGroupDto: CreateGroupDto) {\n    const data = this.groupRepository.create(createGroupDto);\n    const group = await this.groupRepository.findOne({ where: { name: createGroupDto.name } });\n    if (group) {\n      return HttpStatusError.fail('\u8be5\u5206\u7ec4\u5df2\u5b58\u5728');\n    }\n    try {\n      const res = await this.groupRepository.save(data, { reload: true });\n      return res;\n    } catch (error) {\n      return HttpStatusError.fail(error);\n    }\n  }\n}<\/code><\/pre>\n<p><strong>\u5f02\u5e38\u62e6\u622a\u5668\u5c01\u88c5\uff1a<\/strong><\/p>\n<pre><code class=\"js\">import {\n    ArgumentsHost,\n    Catch,\n    ExceptionFilter,\n    HttpException,\n  } from '@nestjs\/common';\n  \n  @Catch()\n  export class HttpExceptionFilter implements ExceptionFilter {\n    catch(exception: HttpException, host: ArgumentsHost) {\n      const ctx = host.switchToHttp();\n      const response = ctx.getResponse();\n      const request = ctx.getRequest();\n  \n      const status = exception.getStatus();\n      const exceptionRes: any = exception.getResponse();\n      const { error, message } = exceptionRes;\n  \n      const msgLog = {\n        status,\n        message,\n        error,\n        path: request.url,\n        timestamp: new Date().toLocaleString(),\n      };\n  \n      response.status(status).json(msgLog);\n    }\n  }\n  <\/code><\/pre>\n<p><strong>\u4f7f\u7528\uff1a<\/strong><\/p>\n<pre><code class=\"js\"> app.useGlobalFilters(new HttpExceptionFilter()); \/\/ \u5168\u5c40\u5f02\u5e38\u8fc7\u6ee4\u5668<\/code><\/pre>\n<p><strong>\u8bf7\u6c42\uff1a<\/strong><\/p>\n<p><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044162288\" alt=\"7-9.png\" title=\"7-9.png\" loading=\"lazy\"><\/p>\n<p>\u8fd9\u6837\u62a5\u9519\u4fe1\u606f\u5c31\u80fd\u591f\u4e00\u76ee\u4e86\u7136\uff0c\u7b80\u5355\u5b9e\u7528\u7684\u8bdd\u53ef\u4ee5\u76f4\u63a5\u629b\u51fa\u5f02\u5e38\u5c31\u53ef\u4ee5\uff0c\u7136\u540e\u5728\u629b\u51fa\u5f02\u5e38\u7684\u5730\u65b9\u7ed9\u51fa\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<h3>\u5168\u5c40\u54cd\u5e94\u62e6\u622a\u5668<\/h3>\n<p>\u90a3\u6210\u529f\u7684\u54cd\u5e94\u5e94\u8be5\u5982\u4f55\u4f18\u96c5\u5730\u5904\u7406\u5462\uff1f<\/p>\n<p><strong>Interceptor\u62e6\u622a\u5668<\/strong><\/p>\n<p>\u8fd9\u91cc\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528<code>Interceptor<\/code>\u62e6\u622a\u5668\uff0c\u7ed9\u6210\u529f\u54cd\u5e94\u6309\u56fa\u5b9a\u683c\u5f0f\u8fd4\u56de<\/p>\n<pre><code class=\"js\">import { Injectable, HttpException, HttpStatus, NestInterceptor, ExecutionContext,CallHandler } from '@nestjs\/common'\nimport { Observable } from 'rxjs'\nimport { map } from 'rxjs\/operators'\n\n\n@Injectable()\nexport class HttpStatusSuccess implements NestInterceptor{\n    intercept(context: ExecutionContext, next: CallHandler) :Observable&lt;any&gt; {\n        return next.handle().pipe(map(data =&gt; {\n            return {\n                statusCode: HttpStatus.OK,\n                message: '\u8bf7\u6c42\u6210\u529f',\n                data\n            }\n        }))\n    }\n}<\/code><\/pre>\n<p><strong>\u4f7f\u7528\uff1a<\/strong><\/p>\n<pre><code class=\"js\"> app.useGlobalInterceptors(new HttpStatusSuccess()); \/\/ \u5168\u5c40\u62e6\u622a\u5668\u8bf7\u6c42\u6210\u529f<\/code><\/pre>\n<p><strong>\u8bf7\u6c42\uff1a<\/strong><br \/><img decoding=\"async\"   referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000044162289\" alt=\"7-10.png\" title=\"7-10.png\" loading=\"lazy\"><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044162278\">https:\/\/segmentfault.com\/a\/1190000044162278<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 Node\u4f5c\u4e3a\u4e00\u95e8\u540e\u7aef\u8bed\u8a00\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u8fde\u63a5\u6570\u636e\u5e93\uff0c\u4e3a\u524d\u7aef\u63d0\u4f9bCURD\u63a5\u53e3 \u6211\u4eec\u4ee5mysql\u4e3a\u4f8b\uff0c\u81ea\u884c\u5b89\u88c5m [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008191","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008191"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008191\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}