{"id":10006646,"date":"2022-10-07T12:05:49","date_gmt":"2022-10-07T04:05:49","guid":{"rendered":"https:\/\/ityet.com\/?p=10006646"},"modified":"2022-10-07T12:05:49","modified_gmt":"2022-10-07T04:05:49","slug":"nest-js%e5%bf%ab%e9%80%9f%e5%90%af%e5%8a%a8api%e9%a1%b9%e7%9b%ae","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006646","title":{"rendered":"Nest.js\u5feb\u901f\u542f\u52a8API\u9879\u76ee"},"content":{"rendered":"<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc2Opd\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<p>\u6700\u8fd1\u4e0a\u4e86\u4e00\u4e2a\u65b0\u9879\u76ee\uff0c\u8fd9\u4e2a\u5ba2\u6237\u7ba1\u7406\u4e00\u4e2a\u5e9e\u5927\u7684\u4efb\u52a1\u548c\u56e2\u961f\u96c6\u7fa4\uff0c\u800c\u4e0d\u540c\u6d41\u7a0b\u6240\u9002\u7528\u7684\u7cfb\u7edf\u4e5f\u4e0d\u592a\u4e00\u6837\uff0c\u6bd4\u5982salesforce\uff0chubspots\u4e4b\u7c7b\u7684\u3002\u8fd9\u6b21\u7684\u65b0\u9879\u76ee\u9700\u8981\u5728\u53e6\u5916\u4e24\u4e2a\u5e73\u53f0\u4e4b\u95f4\u505a\u4e00\u4e9b\u4e8b\u60c5\u3002\u76ee\u524d\u53ea\u9700\u8981\u5148\u5c01\u88c5\u5176\u4e2d\u4e4b\u4e00\u7684API\uff0c\u56e0\u6b64\u6211\u4eec\u9009\u5b9a\u4f7f\u7528NodeJS\u7684\u6846\u67b6<a href=\"https:\/\/link.segmentfault.com\/?enc=OC2lIwa1Oq5m7Rz4YuZMuA%3D%3D.4XudUygo%2FN%2FUXFcoleoJVkGVCcUJGW6%2BceE%2B4z7Cnbo%3D\" rel=\"nofollow\">Nest.js<\/a>\u6765\u5b9e\u73b0\u8fd9\u5957API\u3002<\/p>\n<h2>\u5feb\u901f\u542f\u52a8<\/h2>\n<p>\u5f00\u542fnestjs\u9879\u76ee\u67093\u79cd\u4fbf\u6377\u7684\u65b9\u5f0f\u3002<\/p>\n<h6>\u4f7f\u7528nest\u81ea\u5e26\u7684\u547d\u4ee4\u884c\u5de5\u5177<\/h6>\n<pre><code>npm i -g @nestjs\/cli\nnest new project-name<\/code><\/pre>\n<p>\u5373\u4f7f\u4e0d\u4f7f\u7528\u8fd9\u79cd\u65b9\u5f0f\uff0c\u4e5f\u5efa\u8bae\u5728node\u5168\u5c40\u5b89\u88c5\u547d\u4ee4\u884c\u5de5\u5177\uff0c\u8fd9\u6837\u53ef\u4ee5\u65b9\u4fbf\u7684\u751f\u6210\u5404\u79cdnestjs\u7684\u6a21\u5757\uff0c\u6bd4\u5982controller\u548cservice\u4e4b\u7c7b\u7684\u3002<\/p>\n<h6>\u76f4\u63a5\u4f7f\u7528starter\u9879\u76ee<\/h6>\n<p>\u8fd9\u91cc\u8fd8\u6709\u4e00\u4e2a\u7528\u4e8e\u542f\u52a8\u7684\u6837\u4f8b\u9879\u76ee\uff0c\u53ef\u4ee5\u76f4\u63a5\u4f7f\u7528\u3002<\/p>\n<pre><code>git clone https:\/\/github.com\/nestjs\/typescript-starter.git my-app\ncd my-app\nnpm install\nnpm run start<\/code><\/pre>\n<p>\u800c\u4e14\u8fd9\u4e2a\u9879\u76ee\u8fd8\u9644\u5e26\u4e86Typescript\u3002\u4e0d\u8fc7\u8981\u8bb0\u5f97\u628a\u4e4b\u524d\u7684git\u4fe1\u606f\u5220\u6389\u3002<\/p>\n<h6>\u7528npm\u5b89\u88c5\u6240\u9700\u7684\u5305<\/h6>\n<pre><code>npm i --save @nestjs\/core @nestjs\/common rxjs reflect-metadata<\/code><\/pre>\n<p>\u76f4\u63a5\u5b89\u88c5nestjs\u7684core\u548ccommon\u5c31\u53ef\u4ee5\uff0crxjs\u548creflext-metadata\u4e5f\u662f\u5fc5\u9700\u7684\u3002<\/p>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u6bd4\u8f83\u5e72\u51c0\uff0c\u76ee\u5f55\u4ec0\u4e48\u7684\u9700\u8981\u81ea\u5df1\u521b\u5efa\u3002\u4e0d\u8fc7\u4e5f\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4\u884c\u521b\u5efacontroller\u4e4b\u7c7b\u7684\uff0c\u76ee\u5f55\u4f1a\u81ea\u52a8\u521b\u5efa\u597d\u3002<\/p>\n<p>\u603b\u7684\u6765\u8bf4\uff0cnestjs\u548c\u5176\u4ed6\u8bed\u8a00API\u6846\u67b6\u7c7b\u4f3c\uff0c\u5f88\u591a\u4e1c\u897f\u53ef\u4ee5\u81ea\u52a8\u751f\u6210\u6216\u8005\u65e0\u9700\u7f16\u5199\uff0c\u6240\u4ee5\u7ea6\u5b9a\u7684\u4e60\u60ef\u975e\u5e38\u91cd\u8981\uff0c\u5c3d\u91cf\u4e0d\u8981\u521b\u5efa\u4e00\u4e9b\u201c\u72ec\u7279\u201d\u7684\u7ed3\u6784\uff0c\u907f\u514d\u4ee5\u540e\u8e29\u5751\u3002<\/p>\n<h3>\u521b\u5efacontroller<\/h3>\n<p>\u521b\u5efa\u597d\u9879\u76ee\u4e4b\u540e\uff0c\u6211\u521b\u5efa\u4e86\u4e00\u4e2acontroller\uff0cnest.js\u4e2dcontroller\u53ef\u4ee5\u901a\u8fc7\u4fee\u9970\u5668\u76f4\u63a5\u63d0\u4f9b\u8def\u7531\uff0c\u56e0\u6b64\u6ca1\u6709\u4e00\u4e2aroute\u4e4b\u7c7b\u7684\u6587\u4ef6\u7528\u4e8e\u914d\u7f6e\u3002<\/p>\n<pre><code>nest g controller projects<\/code><\/pre>\n<p>nest.js\u7684\u76ee\u5f55\u7ea6\u5b9a\u662f\u6309\u4e1a\u52a1\u6a21\u5757\u5212\u5206\uff0c\u56e0\u4e3asrc\u76ee\u5f55\u4e2d\u4f1a\u51fa\u73b0\u4e00\u4e2aprojects\u7684\u76ee\u5f55\uff0c\u8be5\u76ee\u5f55\u4e0b\u4f1a\u751f\u6210\u4e00\u4e2aprojects.controller\uff0c\u4ee5\u53ca\u9644\u5e26\u7684\u5355\u5143\u6d4b\u8bd5\u3002<\/p>\n<h3>\u521b\u5efaservice<\/h3>\n<p>\u63a5\u7740\u521b\u5efaservice\uff0c\u7528\u4e8e\u5c01\u88c5\u76ee\u6807\u4efb\u52a1\u7ba1\u7406\u5e73\u53f0\u5173\u4e8eProjects\u7684API\u3002<\/p>\n<pre><code>nest g service projects<\/code><\/pre>\n<p>\u521b\u5efacontroller\u548cservice\u90fd\u4f1a\u81ea\u52a8\u52a0\u5165\u5230module\u91cc\uff0c\u53ef\u4ee5\u5728\u6bcf\u6b21\u751f\u6210\u540e\u7528git diff\u67e5\u770b\u4e00\u4e0b\u751f\u6210\u4e86\u54ea\u4e9b\u4ee3\u7801\uff0c\u4e5f\u597d\u5fc3\u7406\u6709\u6570\u3002<\/p>\n<pre><code>import { Controller, Get, Req } from '@nestjs\/common';\nimport { Request } from 'express';\nimport { Project } from 'src\/interfaces\/project.interface';\nimport { ProjectsService } from '.\/projects.service';\n\n@Controller('projects')\nexport class ProjectsController {\n  constructor(private projectsService: ProjectsService) {}\n\n  @Get()\n  findAll(@Req() request: Request): Project[] {\n    return this.projectsService.findAll();\n  }\n}<\/code><\/pre>\n<pre><code>import { Injectable } from '@nestjs\/common';\nimport { Project } from 'src\/interfaces\/project.interface';\n\n@Injectable()\nexport class ProjectsService {\n  findAll(): Project[] {\n    return [];\n  }\n}<\/code><\/pre>\n<h2>\u7ed3\u6784\u548c\u547d\u540d<\/h2>\n<p>\u53e6\u5916\u6211\u7279\u522b\u60f3\u8bf4\u660e\u4e00\u4e0b\u7684\u662f\uff0c\u867d\u7136\u6211\u4e3acontroller\u548cservice\u90fd\u4f7f\u7528\u4e86\u76f8\u540c\u7684\u540d\u79f0\uff0c\u4f46\u5e76\u4e0d\u662f\u8bf4\u4ed6\u4eec\u662f\u4e00\u4e00\u5bf9\u5e94\u7684\u3002\u5f88\u591a\u9879\u76ee\u53ea\u662f\u4e3a\u4e86\u5206\u5c42\u800c\u5206\uff0ccontroller\u548cservice\u90fd\u662f\u4e00\u4e00\u5bf9\u5e94\u7684\uff0c\u5176\u5b9e\u5e76\u4e0d\u6b63\u786e\u3002\u5206\u5c42\u662f\u56e0\u4e3a\u5b83\u4eec\u62e5\u6709\u4e0d\u540c\u7684\u610f\u4e49\uff0c\u53ea\u6709\u660e\u786e\u8bed\u4e49\uff0c\u624d\u80fd\u5728\u601d\u7ef4\u7684\u8fc7\u7a0b\u4e2d\u66f4\u597d\u7684\u638c\u63e1\u4ee3\u7801\uff0c\u4e5f\u53ef\u4ee5\u66f4\u597d\u7684\u590d\u7528\uff0c\u5c42\u6b21\u8d77\u5230\u7684\u662f\u4e00\u79cd\u8ba4\u77e5\u8f6c\u6362\u7684\u4f5c\u7528\u3002\u5982\u679c\u53ea\u662f\u628a\u5e95\u5c42\u7684\u5bf9\u8c61\u6beb\u65e0\u53d8\u5316\u7684\u6620\u5c04\u51fa\u6765\uff0c\u90a3\u8fd9\u4e2a\u8fc7\u7a0b\u662f\u6beb\u65e0\u610f\u4e49\u7684\u3002<\/p>\n<p>\u8fd9\u91cc\u7684service\u5728nestjs\u4e2d\u5176\u5b9e\u662fprovider\u7684\u4e00\u79cd\uff0c\u800cprovider\u7684\u610f\u4e49\u5219\u662f\u4ece\u5404\u79cd\u4e0d\u540c\u7684\u5730\u65b9\u63d0\u4f9b\u6570\u636e\u6216\u5176\u4ed6\u4e1c\u897f\u3002\u6211\u4f7f\u7528\u7684ProjectsService\u610f\u4e49\u5728\u4e8e\u5c01\u88c5\u53e6\u4e00\u4e2aAPI\uff0c\u6240\u4ee5\u8fd9\u4e2aprojects\u6765\u6e90\u4e8e\u76ee\u6807\u4efb\u52a1\u7ba1\u7406\u5e73\u53f0\u7684API\u540d\u79f0\u3002\u800ccontroller\u7684\u540d\u79f0projects\u6307\u7684\u662f\u6211\u521b\u5efa\u7684API\u6240\u8981\u63d0\u4f9b\u7684\u6570\u636e\u662fproject\uff0c\u53ea\u4e0d\u8fc7\u5b83\u4eec\u5728\u8fd9\u91cc\u786e\u5b9e\u662f\u540c\u4e00\u4e2a\u4e1c\u897f\uff0c\u6240\u4ee5\u540d\u79f0\u4e5f\u4e00\u6837\u3002<\/p>\n<p>\u5047\u8bbe\uff0c\u73b0\u5728\u7684\u4e1a\u52a1\u903b\u8f91\u662f\u9700\u8981\u4ece\u76ee\u6807\u4efb\u52a1\u7ba1\u7406\u5e73\u53f0\u83b7\u53d6projects\uff0c\u4e4b\u540e\u8fc7\u6ee4\u51fa\u4e24\u79cd\u4e0d\u540c\u7279\u6027\u7684projects\uff0c\u4e00\u79cd\u53ebtask\u4efb\u52a1\uff0c\u9700\u8981\u5206\u914d\u7ed9\u4eba\u5458\uff1b\u53e6\u4e00\u79cd\u53ebnote\u8bb0\u5f55\uff0c\u53ea\u662f\u6807\u8bb0\u4e00\u4e0b\u3002\u5b83\u4eec\u62e5\u6709\u4e0d\u540c\u7684\u7279\u6027\u3002\u90a3\u4e48\u6211\u5c31\u4f1a\u521b\u5efa2\u4e2acontroller\uff0ctaskController\u548cnoteController\uff0c\u4f46\u662f\u5b83\u4eec\u90fd\u8c03\u7528ProjectsService\u53bb\u4f7f\u7528\u4e0d\u540c\u7684\u8fc7\u6ee4\u6761\u4ef6\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<h2>HTTP\u8bf7\u6c42<\/h2>\n<p>\u4f7f\u7528nest.js\u5b98\u65b9\u7684Http\u6a21\u5757<code>HttpModule<\/code>\u5c31\u53ef\u4ee5\u5411\u5176\u4ed6API\u53d1\u51fa\u8bf7\u6c42\u3002\u8be5\u6a21\u5757\u5176\u5b9e\u4e5f\u662f\u5c01\u88c5\u7684Axios\uff0c\u6240\u4ee5\u7528\u8d77\u6765\u5f88\u65b9\u4fbf\u3002\u5148\u5b89\u88c5\u76f8\u5173\u6a21\u5757\u3002<\/p>\n<pre><code>npm i --save @nestjs\/axios axios<\/code><\/pre>\n<p>\u7136\u540e\u5728app.module\u4e2d\u5f15\u5165\u3002<\/p>\n<pre><code>import { HttpModule } from '@nestjs\/axios';\n...\n\n@Module({\n  imports: [HttpModule], \/\/ \u5f15\u5165Http\u6a21\u5757\n  controllers: [ProjectsController],\n  providers: [ProjectsService],\n})\nexport class AppModule {}<\/code><\/pre>\n<h6>\u5904\u7406Axios\u5bf9\u8c61<\/h6>\n<p>\u5728service\u4e2d\u53ef\u4ee5\u8fd9\u6837\u5904\u7406http\u8bf7\u6c42\u7684\u8fd4\u56de\u503c\u3002<\/p>\n<pre><code>import { HttpService } from '@nestjs\/axios';\nimport { Injectable } from '@nestjs\/common';\nimport { map, Observable } from 'rxjs';\nimport { Project } from 'src\/interfaces\/project.interface';\nimport { AxiosResponse } from 'axios';\n\n@Injectable()\nexport class ProjectsService {\n  constructor(\n    private readonly httpService: HttpService\n  ) {}\n  findAll(): Observable&lt;Project[]&gt; {\n    return this.httpService.get('http:\/\/localhost:3000\/api-json').pipe(\n      map((axiosResponse: AxiosResponse) =&gt; {\n        return axiosResponse.data;\n      })\n    );\n  }\n}<\/code><\/pre>\n<p>\u56e0\u4e3aAxios\u4f1a\u5305\u88f9\u4e00\u5c42\uff0c\u7528data\u4f5c\u4e3a\u7edf\u4e00\u7684key\uff0c\u6240\u4ee5\u9700\u8981map\u51fa\u6765\u3002\u8fd9\u91cc\u7684pipe\u548cmap\u65b9\u6cd5\u90fd\u662f\u6765\u81earxjs\u3002rxjs\u7684\u6838\u5fc3\u5c31\u662fObservable\uff0c\u4f7f\u7528\u54cd\u5e94\u5f0f\u7f16\u7a0b\u7684\u65b9\u5f0f\uff0c\u5c01\u88c5\u4e86\u56de\u8c03\u548c\u5f02\u6b65\u65b9\u5f0f\u7684\u4ee3\u7801\u3002\u56e0\u6b64\u8fd9\u91cc\u4f60\u770b\u4e0d\u5230promise\u6216\u8005await\/async\u4e4b\u7c7b\u7684\u5173\u952e\u5b57\u3002<\/p>\n<h2>\u914d\u7f6e<\/h2>\n<p>\u4e0a\u9762\u7684\u8bf7\u6c42\uff0c\u6211\u53ea\u662f\u4f7f\u7528\u4e86\u4e00\u4e2a\u672c\u5730\u7684json\u63a5\u53e3\u7528\u4e8e\u6d4b\u8bd5\u3002\u8981\u771f\u6b63\u7684\u8c03\u7528\u76ee\u6807\u5e73\u53f0\u7684API\uff0c\u8fd8\u9700\u8981\u914d\u7f6e\u9879\uff0c\u56e0\u4e3a\u5305\u62ecAPI token\u8fd9\u79cd\u91cd\u8981\u5b57\u7b26\u4e32\u7684\u4e00\u4e9b\u503c\uff0c\u9700\u8981\u653e\u5728\u73af\u5883\u53d8\u91cf\u4e2d\uff0c\u4e0d\u80fd\u76f4\u63a5\u653e\u5728\u4ee3\u7801\u88abgit\u63d0\u4ea4\u3002<\/p>\n<p>\u6240\u4ee5\u6211\u9700\u8981\u52a0\u4e0aconfig\u7684\u914d\u7f6e\uff0c\u5b89\u88c5nest.js\u7684config\u5305\uff0c\u5b83\u5c01\u88c5\u7684\u5176\u5b9e\u662fdotenv\u5305\uff0c\u7ecf\u5e38\u4f7f\u7528nodejs\u7684\u8bdd\uff0c\u5e94\u8be5\u4f1a\u5f88\u719f\u6089\u8fd9\u4e2a\u5305\u3002<\/p>\n<pre><code>npm i --save @nestjs\/config<\/code><\/pre>\n<p>\u540c\u6837\u5728app.module\u4e2d\u5f15\u5165config\u6a21\u5757\u3002<\/p>\n<pre><code>import { ConfigModule } from '@nestjs\/config';\nimport configuration from '.\/config\/configuration';\n...\n  imports: [\n    HttpModule,\n    ConfigModule.forRoot({\n      load: [configuration],\n    })\n  ],\n...<\/code><\/pre>\n<p>\u8fd9\u91cc\u4f7f\u7528forRoot\u662f\u56e0\u4e3a\u8be5\u6a21\u5757\u662f\u5355\u4f8b\u6a21\u5f0f\u7684\u3002\u800c\u4f20\u5165\u7684\u53c2\u6570<code>load<\/code>\u53ef\u4ee5\u628aconfig\u5bf9\u8c61\u8f7d\u5165\u3002<\/p>\n<p>\u5f15\u5165\u7684<code>config\/configuration<\/code>\u6587\u4ef6\u662f\u65b0\u521b\u5efa\u7684\u914d\u7f6e\u5bf9\u8c61\u3002<\/p>\n<pre><code>export default () =&gt; ({\n  port: parseInt(process.env.PORT, 10) || 3000,\n  runn: {\n    url: process.env.RUNN_API_URL,\n    token: process.env.RUNN_API_TOKEN\n  }\n});<\/code><\/pre>\n<p>\u6211\u914d\u7f6e\u4e86\u7aef\u53e3\uff0c\u4ee5\u53caAPI\u7684URL\u548cToken\u3002<\/p>\n<p>\u7136\u540e\u53ef\u80fd\u9700\u8981\u7528\u5230Typescript\u7684\u63a5\u53e3\uff0c\u53ef\u4ee5\u4f7f\u7528nest\u751f\u6210\u6587\u4ef6\u3002<\/p>\n<pre><code>nest g interface runn-config<\/code><\/pre>\n<pre><code>export interface RunnConfig {\n  url: string\n  token: string\n}<\/code><\/pre>\n<p>\u5728service\u4e2d\u5c31\u53ef\u4ee5\u83b7\u53d6\u8fd9\u4e9b\u914d\u7f6e\u9879\u3002<\/p>\n<pre><code>import { ConfigService } from '@nestjs\/config';\nimport { RunnConfig } from 'src\/interfaces\/runn-config.interface';\n\n...\nconstructor(\nprivate readonly httpService: HttpService,\nprivate configService: ConfigService\n) {}\n\n...\nconst config = this.configService.get&lt;RunnConfig&gt;('runn');<\/code><\/pre>\n<p>\u4e0d\u8981\u5fd8\u8bb0\u5728\u6839\u76ee\u5f55\u4e0b\u521b\u5efa<code>.env<\/code>\u6587\u4ef6\u586b\u5165\u914d\u7f6e\u7684\u503c\u3002\u53e6\u5916\u6309\u4e60\u60ef\uff0c\u53ef\u4ee5\u521b\u5efa.env.sample\u6587\u4ef6\uff0c\u53ea\u5305\u542bkey\uff0c\u6ca1\u6709\u503c\uff0c\u7c7b\u4f3c\u6a21\u677f\uff0c\u88abgit\u63d0\u4ea4\u7ba1\u7406\u3002\u800c.env\u6587\u4ef6\u5219\u88abgitignore\u3002\u53ea\u5728\u672c\u5730\u4fdd\u7559\u3002\u5728\u670d\u52a1\u5668\u4e0a\u9700\u8981\u53e6\u5916\u751f\u6210\u4e00\u4efd\u3002<\/p>\n<h4>\u5168\u5c40\u6dfb\u52a0headers<\/h4>\n<p>URL\u4f7f\u7528\u4e86\uff0c\u4f46Token\u9700\u8981\u5728headers\u4e2d\u6dfb\u52a0\u3002\u5728app.module\u4e2d\u4f7f\u7528HttpModule\u7684register\u65b9\u6cd5\u5c31\u53ef\u4ee5\u914d\u7f6e\u5176\u4e2d\u5c01\u88c5\u7684Axios\u3002\u4e0d\u8fc7\u7531\u4e8etoken\u6765\u81eaconfig\uff0c\u6240\u4ee5\u9700\u8981\u7528\u6bd4\u8f83\u9ebb\u70e6\u7684registerAsync\uff0c\u6ce8\u5165config\u540e\uff0c\u5728useFactory\u4e2d\u4f7f\u7528\u3002<\/p>\n<pre><code>...\n  imports: [\n    HttpModule.registerAsync({\n      imports: [ConfigModule],\n      inject: [ConfigService],\n      useFactory: (configService: ConfigService) =&gt; ({\n        headers: {\n          'Content-Type': 'application\/json',\n          Authorization: 'Bearer ' + configService.get('runn.token')\n        }\n      })\n    }),\n    ...\n  ],\n...<\/code><\/pre>\n<p>\u8fd9\u6837\u6211\u5c31\u521b\u5efa\u4e86\u4e00\u4e2a\u57fa\u672cAPI\u6846\u67b6\uff0c\u5e76\u8bf7\u6c42\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u76ee\u6807\u4efb\u52a1\u7ba1\u7406\u7cfb\u7edf\u7684API\u83b7\u53d6\u6570\u636e\u3002<\/p>\n<h2>API\u6587\u6863<\/h2>\n<p>\u53e6\u5916\uff0c\u7531\u4e8e\u5ba2\u6237\u9700\u8981\u4e86\u89e3\u548c\u6d4b\u8bd5\u6211\u4eec\u7684API\uff0c\u6240\u4ee5\u9700\u8981\u4e00\u4e2apostman\u7684api\u96c6\u5408\u3002\u6211\u51c6\u5907\u4f7f\u7528Swagger\uff0c\u8fd9\u662f\u4e00\u4e2aAPI\u6587\u6863\u7684\u81ea\u52a8\u751f\u6210\u5de5\u5177\uff0c\u5b83\u4f1a\u6839\u636e\u6846\u67b6\u4e2d\u5b9a\u4e49\u7684API\u548c\u53c2\u6570\uff0c\u81ea\u52a8\u751f\u6210\u4e00\u4e2a\u9875\u9762\uff0c\u5305\u542b\u6240\u6709\u7684API\u548c\u53c2\u6570\u8bf4\u660e\uff0c\u4ee5\u53ca\u53ef\u4ee5\u76f4\u63a5\u8bf7\u6c42\u8be5API\u3002\u5f53\u7136\u8fd9\u9700\u8981\u4fee\u9970\u5668\u7684\u8f85\u52a9\u3002\u5148\u5b89\u88c5nestjs\u7684swagger\u5305\u3002<\/p>\n<pre><code>npm i --save @nestjs\/swagger<\/code><\/pre>\n<p>\u7136\u540e\u5728<code>main.ts<\/code>\u4e2d\u5f15\u5165\u5e76\u914d\u7f6e\u3002<\/p>\n<pre><code>\nimport { NestFactory } from '@nestjs\/core';\nimport { SwaggerModule, DocumentBuilder } from '@nestjs\/swagger';\nimport { AppModule } from '.\/app.module';\n\nasync function bootstrap() {\n  const app = await NestFactory.create(AppModule);\n\n  \/\/ swagger\n  const config = new DocumentBuilder()\n    .setTitle('My APIs')\n    .setDescription('My APIs documents')\n    .setVersion('1.0')\n    .build();\n  const document = SwaggerModule.createDocument(app, config);\n  SwaggerModule.setup('api', app, document);\n  \/\/ swagger end\n\n  await app.listen(3000);\n}\nbootstrap();<\/code><\/pre>\n<p>\u7136\u540e\u5c31\u53ef\u4ee5\u901a\u8fc7<code>http:\/\/localhost:3000\/api<\/code>\u8bbf\u95ee\u8be5API\u6587\u6863\u9875\u9762\u3002\u4e0d\u8fc7\u76ee\u524d\u6240\u6709API\u90fd\u4f1a\u51fa\u73b0\u5728default\u9ed8\u8ba4\u6807\u7b7e\u4e0b\u3002\u5b98\u65b9\u793a\u4f8b\u4e2d\u4f7f\u7528\u7684addTag\u65b9\u6cd5\u867d\u7136\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u6807\u7b7e\uff0c\u4f46\u5e76\u4e0d\u80fd\u6307\u5b9a\u67d0\u4e9bAPI\u653e\u5165\u8be5\u6807\u7b7e\u3002\u6211\u9700\u8981\u901a\u8fc7\u4fee\u9970\u5668\u5b9e\u73b0\u3002<\/p>\n<p>\u5728controller\u7684\u65b9\u6cd5\u4e0a\u4f7f\u7528<code>@ApiTags('Project')<\/code>\u5373\u53ef\uff0c\u8be5\u65b9\u6cd5\u4f1a\u88ab\u653e\u7f6e\u5728Project\u6807\u7b7e\u4e0b\u3002<\/p>\n<p>\u9664\u4e86API\u6587\u6863\u7684\u9875\u9762\u5f62\u5f0f\u3002<code>http:\/\/localhost:3000\/api-json<\/code>\u7684JSON\u683c\u5f0f\u624d\u662f\u6700\u91cd\u8981\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b83\u5728Postman\u4e2d\u76f4\u63a5\u5bfc\u5165\u3002<\/p>\n<p><a href=\"https:\/\/link.segmentfault.com\/?enc=2l%2FSrW0hc4S3tkikOg3wOA%3D%3D.3%2BUaBVnZlgDL%2FOepaKWZlGEJ3zNepaZLhP9KiT6B8IfZf2eBQUNRgnl04As0ztXn\" rel=\"nofollow\">\u539f\u535a\u5ba2\u8fde\u63a5<\/a><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042577078\">https:\/\/segmentfault.com\/a\/1190000042577078<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u4e0a\u4e86\u4e00\u4e2a\u65b0\u9879\u76ee\uff0c\u8fd9\u4e2a\u5ba2\u6237\u7ba1\u7406\u4e00\u4e2a\u5e9e\u5927\u7684\u4efb\u52a1\u548c\u56e2\u961f\u96c6\u7fa4\uff0c\u800c\u4e0d\u540c\u6d41\u7a0b\u6240\u9002\u7528\u7684\u7cfb\u7edf\u4e5f\u4e0d\u592a\u4e00\u6837\uff0c\u6bd4\u5982salesfo [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006646","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006646","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"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006646"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006646\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006646"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006646"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006646"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}