{"id":703,"date":"2023-11-11T09:01:01","date_gmt":"2023-11-11T01:01:01","guid":{"rendered":""},"modified":"2023-11-11T09:01:01","modified_gmt":"2023-11-11T01:01:01","slug":"\u540e\u7aef\u95ee\uff1a\u4f5c\u4e3a\u524d\u7aef\u5982\u4f55\u5b9e\u73b0\u81ea\u52a8\u751f\u6210\u63a5\u53e3\u6587\u6863","status":"publish","type":"post","link":"https:\/\/mushiming.com\/703.html","title":{"rendered":"\u540e\u7aef\u95ee\uff1a\u4f5c\u4e3a\u524d\u7aef\u5982\u4f55\u5b9e\u73b0\u81ea\u52a8\u751f\u6210\u63a5\u53e3\u6587\u6863"},"content":{"rendered":"

\u524d\u8a00\uff0c\u4f17\u6240\u5468\u77e5\uff0c\u540e\u7aef\u7684\u63a5\u53e3\u6587\u6863\u4e00\u822c\u4f7f\u7528 swagger\/openapi\uff0c\u800c\u5176\u4ed6\u7684\u4e00\u822c\u63a5\u53e3\u6587\u6863\u4e5f\u57fa\u672c\u662f\u4f7f\u7528 openapi \u6765\u8fdb\u884c\u89e3\u6790\u7684\u3002\u4f8b\u5982 redoc(20.8k star) \u548c knife4j(3.5k star) \u7b49.<\/p>\n

\u4f5c\u4e3a\u4e00\u4e2a\u901a\u7528\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u524d\u7aef\u793e\u533a\u81ea\u7136\u4e5f\u6709\u5bf9\u5e94\u7684\u5b9e\u73b0\u3002<\/p>\n

\u5982\u4f55\u5b9e\u73b0 swagger \u6587\u6863\u754c\u9762<\/h2>\n

swagger-ui-express \u662f\u4e00\u4e2a express \u63d2\u4ef6\uff0c\u53ef\u4ee5\u5728 express \u4e2d\u5b9e\u73b0 swagger-ui\u3002\u7136\u540e\u4f20\u5165 openapi \u6765\u6e32\u67d3\u63a5\u53e3\u6587\u6863\u3002<\/p>\n

const<\/span> express = require<\/span>(\"express\"<\/span>);\nconst<\/span> app = express<\/span>();\nconst<\/span> swaggerUi = require<\/span>(\"swagger-ui-express\"<\/span>);\nconst<\/span> swaggerDocument = require<\/span>(\".\/swagger.json\"<\/span>);\n\nvar<\/span> options = {\n  explorer<\/span>: true<\/span>,\n};\n\napp.use<\/span>(\n  \"\/api-docs\"<\/span>,\n  swaggerUi.serve<\/span>,\n  swaggerUi.setup<\/span>(swaggerDocument, options)\n);\n<\/code><\/pre>\n

\u4ece\u4e0a\u9762\u7684\u793a\u4f8b\u53ef\u4ee5\u770b\u5230 swagger.json \u5373\u4e3a\u63a5\u53e3\u6587\u6863\u7684\u63cf\u8ff0\u6570\u636e\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\u6211\u4eec\u9700\u8981\u51c6\u5907\u5df2\u7ecf\u6709\u7684 json \u6570\u636e\u65b9\u53ef\u663e\u793a\u6587\u6863\u3002<\/p>\n

\u5982\u4f55\u751f\u6210\u6587\u6863\u63cf\u8ff0\u6570\u636e<\/h2>\n

\u6211\u4eec\u53ef\u4ee5\u5b89\u6392 swagger-jsdoc \u8fd9\u4e2a\u5e93\uff0c\u6765\u5b9e\u73b0\u5728\u4ee3\u7801\u4e2d\u5199\u63a5\u53e3\u6ce8\u91ca\uff0c\u5373\u53ef\u751f\u6210\u6587\u6863\u6570\u636e\u3002<\/p>\n

\/\/ Initialize swagger-jsdoc -> returns validated swagger spec in json format<\/span>\nconst<\/span> swaggerSpec = swaggerJSDoc<\/span>(options);\n\napp.use<\/span>(\"\/api-docs\"<\/span>, swaggerUi.serve<\/span>, swaggerUi.setup<\/span>(swaggerSpec));\n<\/code><\/pre>\n

\u6ce8\u91ca\u793a\u4f8b\uff1a<\/p>\n

\/** * @openapi<\/span> * \/: * get: * description: Welcome to swagger-jsdoc! * responses: * 200: * description: Returns a mysterious string. *\/<\/span>\napp.get<\/span>(\"\/\"<\/span>, (req, res<\/span>) =><\/span> {\n  res.send<\/span>(\"Hello World!\"<\/span>);\n});\n<\/code><\/pre>\n

\u6587\u6863\u4e0d\u60f3\u5199\uff0c\u6ce8\u91ca\u4e0d\u60f3\u5199\uff0c\u5c31\u60f3\u751f\u6210\u63a5\u53e3\u6587\u6863<\/h2>\n

\u5728\u4e4b\u524d\u7684\u793a\u4f8b\u4e2d\uff0c\u867d\u7136\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5199 jsdoc \u5f62\u5f0f\u7684\u6ce8\u91ca\u6765\u5b9e\u73b0\u4e5f\u5199\u63a5\u53e3\u6587\u6863\u3002\u4f46\u95ee\u9898\u662f\uff0c\u6211\u90fd\u80fd\u5728\u6ce8\u91ca\u91cc\u628a\u6587\u6863\u5199\u6e05\u695a\u4e86\uff0c\u5c31\u4e0d\u53eb\u201c\u751f\u6210\u6587\u6863\u201d\u4e86\uff0c\u800c\u53eb\u6536\u96c6\u6587\u6863\u7247\u6bb5\uff0c\u800c\u4e14\u5728\u4ee3\u7801\u91cc\u5199\u6587\u6863\uff0c\u4f3c\u4e4e\u4e5f\u4e0d\u592a\u4f18\u96c5\u3002<\/p>\n

\u8981\u5b9e\u73b0\u6ce8\u91ca\u5199\u4e0d\u60f3\u5199\uff0c\u5c31\u80fd\u5b9e\u73b0\u63a5\u53e3\u6587\u6863\u7684\u751f\u6210\uff0c\u6211\u4eec\u9996\u5148\u8981\u8ba9\u67d0\u4e2a\u5de5\u5177\u80fd\u7406\u89e3\u6211\u4eec\u4ee3\u7801\u7684\u610f\u56fe\u3002\u5982\u4f55\u7406\u89e3\uff1f<\/p>\n

    \n
  • \u65b9\u6cd5 1 \u901a\u8fc7\u4eba\u5de5\u667a\u80fd\u53bb\u7406\u89e3\u4ee3\u7801\u610f\u56fe\u7136\u540e\u751f\u6210\u6587\u6863<\/li>\n
  • \u65b9\u6cd5 2 \u901a\u8fc7\u4ee3\u7801\u94a9\u5b50\u6216\u57cb\u70b9\u83b7\u53d6\u610f\u56fe\u751f\u6210\u6587\u6863<\/li>\n<\/ul>\n

    \u5f88\u663e\u7136\uff0c\u65b9\u6cd5 1 \u4e0d\u5728\u6211\u4eec\u7684\u5b9e\u73b0\u8303\u7574\uff0c\u56e0\u4e3a\u5b83\u6d89\u53ca\u5927\u6a21\u578b\u3001\u6570\u636e\u5b89\u5168\u3001\u672c\u5730\u5316\u7b49\u5404\u79cd\u95ee\u9898\u3002\u800c\u65b9\u6cd5 2 \u5219\u662f\u6211\u4eec\u80fd\u53bb\u7814\u7a76\u7684\u65b9\u6848\uff0c\u4f8b\u5982\u6211\u4eec\u4f7f\u7528 vue \u5f00\u53d1\u9879\u76ee\u65f6\uff0cvue-devtool \u63d2\u4ef6\u5c31\u80fd\u770b\u5230 vue \u76f8\u5173\u7684\u72b6\u6001\uff0c\u8fd9\u5c31\u662f\u5728 vue \u7684\u5b9e\u73b0\u4e2d\u6709\u4e3a\u63d2\u4ef6\u5f00\u653e\u83b7\u53d6\u72b6\u6001\u7684\u63a5\u53e3\u3002\u5982\u679c\u6ca1\u6709\u5f00\u653e\uff0c\u4e5f\u53ef\u4ee5\u901a\u8fc7\u62e6\u622a\u3001\u4ee3\u7406\u7b49\u65b9\u6cd5\u83b7\u53d6\u8fd0\u884c\u72b6\u6001\u3002<\/p>\n

    \u5206\u6790\u63a5\u53e3\u610f\u56fe<\/h2>\n

    \u5bf9\u4e8e\u539f\u751f\u7684 express \u4ea7\u751f\u7684\u63a5\u53e3\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7 app.routes<\/code> app._router.stack<\/code> express.Router()<\/code> router.stack<\/code> \u7b49 api \u83b7\u53d6\u5230\u5f53\u524d\u6240\u4f7f\u7528\u7684\u63a5\u53e3\u3002\u4f46\u6211\u4eec\u8fd9\u91cc\u4f7f\u7528 mockm\uff0c\u63a5\u53e3\u662f\u76f4\u63a5\u58f0\u660e\u5728\u5bf9\u8c61\u4e2d\u7684\uff0c\u5c31\u66f4\u7b80\u5355\u4e86\u8bb8\u591a\u3002<\/p>\n

    api<\/span>: {\n  \"\/a\"<\/span>: 111<\/span>,\n  \"\/b\"<\/span>: 222<\/span>,\n  'get \/name'<\/span>: `\u5f20\u4e09`<\/span>,\n  '\/status\/:code'<\/span> (req, res) {\n    res.json<\/span>({statusCode<\/span>: req.params<\/span>.code<\/span>})\n  },\n},\n<\/code><\/pre>\n

    \u89c2\u5bdf\u4e0a\u9762\u7684 api \u5bf9\u8c61\uff0c\u4f8b\u5982 \"\/a\": 111<\/code> \u5b9e\u73b0\u7684\u63a5\u53e3\u662f\u65e0\u8bba\u901a\u8fc7\u4ec0\u4e48\u65b9\u6cd5\u8bf7\u6c42 \/a \u8fd9\u4e2a\u63a5\u53e3\uff0c\u90fd\u8fd4\u56de\u6570\u5b57 111\uff0c\u800c 'get \/name': '\u5f20\u4e09',<\/code> \u5219\u662f\u901a\u8fc7 get \u8bf7\u6c42 \/name \u65f6\u8fd4\u56de\u5b57\u7b26\u4e32 \u5f20\u4e09<\/code>\u3002<\/p>\n

    \u4ee5\u4e0b\u662f\u8f6c\u6362 api \u4e3a openApi json \u7684\u5b9e\u73b0\uff1a<\/p>\n

    toOpenApi<\/span>(serverRouterList<\/span>) {\n  const<\/span> mapObj = {\n    all<\/span>(item<\/span>) {\n      return<\/span> [\n        `delete`<\/span>,\n        `get`<\/span>,\n        `patch`<\/span>,\n        `post`<\/span>,\n        `put`<\/span>,\n      ].map<\/span>(method<\/span> =><\/span> {\n        return<\/span> {\n          ...item,\n          method,\n          tags<\/span>: item.tags<\/span> || [`all-method`<\/span>],\n        }\n      })\n    },\n  }\n  serverRouterList = serverRouterList.map<\/span>(item<\/span> =><\/span> {\n    const<\/span> fn = mapObj[item.method<\/span>]\n    return<\/span> fn ? fn<\/span>(item) : [item]\n  }).flat<\/span>()\n  return<\/span> serverRouterList\n}\n<\/code><\/pre>\n

    \"\u540e\u7aef\u95ee\uff1a\u4f5c\u4e3a\u524d\u7aef\u5982\u4f55\u5b9e\u73b0\u81ea\u52a8\u751f\u6210\u63a5\u53e3\u6587\u6863<\/p>\n

    serverRouterList \u662f\u683c\u5f0f\u5316 api \u540e\u9762\u7684\u6570\u636e\uff0c\u5305\u542b method route action alias \u7b49\uff0c\u5176\u4e2d\u8981\u751f\u6210\u63a5\u53e3\u6587\u6863\uff0c\u53ea\u9700\u8981\u6709 method route \u5c31\u8db3\u591f\u4e86\u3002<\/p>\n

    \u4e00\u4e2a\u53ea\u5305\u542b\u6b64\u63a5\u53e3\u7684 openApi json \u751f\u6210\u540e\u5982\u4e0b\uff1a<\/p>\n

    {<\/span>\n  \"openapi\"<\/span>:<\/span> \"3.0.0\"<\/span>,<\/span>\n  \"servers\"<\/span>:<\/span> [<\/span>\n    {<\/span>\n      \"url\"<\/span>:<\/span> \"\/\"<\/span>\n    }<\/span>\n  ]<\/span>,<\/span>\n  \"tags\"<\/span>:<\/span> [<\/span>]<\/span>,<\/span>\n  \"paths\"<\/span>:<\/span> {<\/span>\n    \"\/a\"<\/span>:<\/span> {<\/span>\n      \"get\"<\/span>:<\/span> {<\/span>\n        \"parameters\"<\/span>:<\/span> [<\/span>]<\/span>,<\/span>\n        \"responses\"<\/span>:<\/span> {<\/span>}<\/span>\n      }<\/span>\n    }<\/span>\n  }<\/span>\n}<\/span>\n<\/code><\/pre>\n

    \u770b\u8d77\u6765\u662f\u7b80\u5355\u5427\uff1f<\/p>\n

    \u7136\u540e\u628a\u5b83\u4ea4\u7ed9 swagger-ui \u5373\u53ef\u6e32\u67d3\u51fa\u6587\u6863\uff0c\u5c55\u73b0\u548c\u4f7f\u7528\u90fd\u6ca1\u6709\u95ee\u9898\uff1a<\/p>\n

    \"\u540e\u7aef\u95ee\uff1a\u4f5c\u4e3a\u524d\u7aef\u5982\u4f55\u5b9e\u73b0\u81ea\u52a8\u751f\u6210\u63a5\u53e3\u6587\u6863<\/p>\n

    \u5982\u4f55\u4e3a\u63a5\u53e3\u5206\u7ec4\u3001\u6dfb\u52a0\u5b57\u6bb5\u8bf4\u660e\u3001\u6570\u636e\u9a8c\u8bc1\u7b49\u529f\u80fd\uff1f<\/h2>\n

    \u63a5\u53e3\u5206\u7ec4\u6bd4\u8f83\u7b80\u5355\uff0c\u76f4\u63a5\u4e3a\u63a5\u53e3\u8bbe\u7f6e\u6807\u7b7e\u5c31\u884c\u3002\u7531\u4e8e\u5728 mockm \u4e2d\u53ef\u4ee5\u76f4\u63a5\u5199\u5b57\u9762\u91cf\u5373\u53ef\u751f\u6210 api\uff0c\u4f46\u6211\u4eec\u8981\u4e3a api \u6dfb\u52a0\u989d\u5916\u8bf4\u660e\u7684\u65f6\u5019\uff0c\u5c31\u4e0d\u80fd\u7b80\u5355\u7684\u4f7f\u7528\u5b57\u9762\u91cf\u4e86\u3002<\/p>\n

    \u4e3a\u4e86\u517c\u5bb9 mockm \u7684\u5df2\u6709\u5b9e\u73b0\uff0c\u6211\u4eec\u6269\u5c55\u4e00\u4e2a\u53eb side \u7684\u51fd\u6570\uff0c\u7528\u4e8e\u6536\u96c6\u989d\u5916\u4fe1\u606f\u548c\u8fd4\u56de\u5b57\u9762\u91cf\u3002<\/p>\n

    \n\"post \/api\/login\"<\/span>: side<\/span>({\n  tags<\/span>: [`admin`<\/span>],\n  summary<\/span>: `\u6839\u636e\u7528\u6237\u540d\u83b7\u53d6 token`<\/span>,\n  schema<\/span>: {\n    body<\/span>: joi.object<\/span>({\n      username<\/span>: joi.string<\/span>()\n        .default<\/span>(`\u674e\u857e`<\/span>)\n        .required()\n        .description<\/span>(`\u7528\u6237\u540d`<\/span>),\n    }).description<\/span>(`\u7528\u6237\u4fe1\u606f`<\/span>),\n  },\n  action (req, res) {\n    const<\/span> { username } = req.body<\/span>\n    res.json<\/span>({\n      status<\/span>: 200<\/span>,\n      message<\/span>: `\u6b22\u8fce ${username}<\/span>, \u767b\u5f55\u6210\u529f`<\/span>,\n      token<\/span>: `tokentoken`<\/span>,\n    });\n  }\n}),\n<\/code><\/pre>\n

    \u53ef\u4ee5\u770b\u5230\uff0c\u6211\u4eec\u5728 side \u51fd\u6570\u4e2d\uff0c\u4e3a api \u8fdb\u884c\u4e86\u5206\u7ec4\uff0c\u5e76\u4e14\u4e3a api \u6dfb\u52a0\u4e86\u63cf\u8ff0\uff0c\u7136\u540e\u4e3a api \u7684\u6bcf\u4e2a\u5b57\u6bb5\u6dfb\u52a0\u4e86\u8bf4\u660e\uff0c\u662f\u5426\u5fc5\u586b\uff0c\u6570\u5b57\u7c7b\u578b\u662f\u4ec0\u4e48\u7b49\u3002<\/p>\n

    \u8fd9\u65f6\u5019\u53ef\u80fd\u6709\u4e00\u4e2a\u7591\u95ee\uff0c\u5728\u524d\u9762\u63d0\u5230\u4e86\u4f7f\u7528\u6ce8\u91ca\u7f16\u5199\u63a5\u53e3\u6587\u6863\u4fe1\u606f\uff0c\u800c\u8fd9\u91cc\u5219\u662f\u4f7f\u7528\u4ee3\u7801\u7f16\u5199\u63a5\u53e3\u4fe1\u606f\uff0c\u4ed6\u4eec\u4e4b\u524d\u7684\u533a\u522b\u5728\u54ea\u91cc\uff1f<\/p>\n

      \n
    • \u533a\u522b 1 \u4f7f\u7528\u6ce8\u91ca\u7f16\u5199\u65f6\u6ca1\u6709\u8bed\u6cd5\u63d0\u793a\u3001\u4ee3\u7801\u5b8c\u6210\u3001\u4ee3\u7801\u9ad8\u4eae\u7b49\u529f\u80fd\uff0c\u5341\u5206\u4e0d\u65b9\u4fbf<\/li>\n
    • \u533a\u522b 2 \u4f7f\u7528\u6ce8\u91ca\u7f16\u5199\u65f6\uff0c\u5b9e\u9645\u4e0a\u662f\u5728\u5199\u539f\u59cb openapi yaml \u683c\u5f0f\u7684\u6570\u636e\uff0c\u8fd9\u8981\u6c42\u4f60\u5bf9 openapi \u89c4\u8303\u6709\u76f8\u5f53\u7684\u4e86\u89e3\u548c\u719f\u6089<\/li>\n
    • \u533a\u522b 3 \u4f7f\u7528\u4ee3\u7801\u7f16\u5199\u65f6\uff0c\u987a\u4fbf\u53ef\u4ee5\u5b8c\u6210\u4e1a\u52a1\u4e0a\u7684\u5176\u4ed6\u529f\u80fd\uff0c\u800c\u6ce8\u91ca\u548c\u4e1a\u52a1\u662f\u5272\u88c2\u7684\uff0c\u6211\u4eec\u90fd\u77e5\u9053\u4e1a\u52a1\u66f4\u65b0\u4f46\u6ce8\u91ca\u6ca1\u66f4\u65b0\u7684\u90a3\u79cd\u5669\u68a6<\/li>\n<\/ul>\n

      \u89c2\u5bdf\u4e0a\u9762\u7684\u4ee3\u7801\uff0c\u6211\u4eec\u4f7f\u7528 joi \u6765\u8fdb\u884c\u6570\u636e\u63cf\u8ff0\uff0c\u4e0e\u6b64\u540c\u65f6 joi \u4e5f\u53ef\u4ee5\u4e3a\u6211\u4eec\u5b9e\u73b0\u6570\u636e\u7684\u6821\u9a8c\u3002<\/p>\n

      \u4ee5\u4e0b\u662f\u6587\u6863\u751f\u6210\u6548\u679c\uff0c\u53ef\u4ee5\u770b\u5230\u6570\u636e\u63cf\u8ff0\u3001\u9ed8\u8ba4\u503c(\u793a\u4f8b\u503c)\u3001\u7c7b\u578b\u3001\u662f\u5426\u5fc5\u586b\u7b49\u90fd\u53ef\u4ee5\u6b63\u5e38\u663e\u793a\uff1a<\/p>\n

      \"\u540e\u7aef\u95ee\uff1a\u4f5c\u4e3a\u524d\u7aef\u5982\u4f55\u5b9e\u73b0\u81ea\u52a8\u751f\u6210\u63a5\u53e3\u6587\u6863<\/p>\n

      \u4ee5\u4e0b\u662f\u63a5\u53e3\u6821\u9a8c\u529f\u80fd\u7684\u6548\u679c\uff0c\u6211\u4eec\u5728\u63a5\u53e3\u4e2d\u58f0\u660e\u4e86 username \u4e3a string\uff0c\u4f46\u6211\u4eec\u5c1d\u8bd5\u4f20\u5165\u6570\u5b57 111\uff0c\u7ed3\u679c\u6309\u9884\u671f\u8fd0\u884c\uff0c\u81ea\u52a8\u63d0\u793a\u4e86\u54ea\u4e2a\u5b57\u6bb5\u9519\u8bef\u4e86\uff0c\u4e3a\u4ec0\u4e48\u9519\uff1a<\/p>\n

      \"\u540e\u7aef\u95ee\uff1a\u4f5c\u4e3a\u524d\u7aef\u5982\u4f55\u5b9e\u73b0\u81ea\u52a8\u751f\u6210\u63a5\u53e3\u6587\u6863<\/p>\n

      \u529f\u80fd\u63d2\u4ef6\u5316<\/h2>\n

      \u5bf9\u4e8e\u4f7f\u7528 mockm \u7684\u7fa4\u4f53\u4e2d\uff0c\u6709\u4e00\u4e9b\u670b\u53cb\u53ef\u80fd\u5e76\u4e0d\u60f3\u53bb\u771f\u7684\u7528\u5b83\u6765\u5b9e\u73b0 api \uff0c\u66f4\u522b\u8bf4\u8fd8\u8981\u4e3a api \u751f\u6210\u6587\u6863\uff0c\u6240\u4ee5\u628a\u6b64\u529f\u80fd\u62bd\u79bb\u4e3a\u63d2\u4ef6\uff0c\u5f53\u6709\u4e00\u5929 mockm \u7684\u529f\u80fd\u7e41\u590d\u65f6\uff0c\u53ef\u4ee5\u62c6\u5206\u4e3a\u63d2\u4ef6\u5305\u5355\u72ec\u5728\u5176\u4ed6\u4ed3\u5e93\u8fdb\u884c\u7ef4\u62a4\uff1a<\/p>\n

      module<\/span>.exports<\/span> = async<\/span> (util) => {\n  const<\/span> joi = await<\/span> util.tool<\/span>.generate<\/span>.initPackge<\/span>(`joi`<\/span>);\n\n  return<\/span> {\n    plugin<\/span>: [util.plugin<\/span>.validate<\/span>, util.plugin<\/span>.apiDoc<\/span>],\n  };\n};\n<\/code><\/pre>\n

      \u6e38\u4e50\u573a<\/h2>\n

      \u5b89\u88c5\u548c\u521d\u59cb\u5316 mockm<\/h3>\n
      yarn add mockm\nnpx mm --config\n<\/code><\/pre>\n

      \u7f16\u5199\u63a5\u53e3<\/h3>\n

      \u5728 mm.config.js \u4e2d\u5f55\u5165\u4ee5\u4e0b\u4fe1\u606f<\/p>\n

      module<\/span>.exports<\/span> = async<\/span> (util) => {\n  const<\/span> joi = await<\/span> util.tool<\/span>.generate<\/span>.initPackge<\/span>(`joi`<\/span>);\n\n  return<\/span> {\n    plugin<\/span>: [util.plugin<\/span>.validate<\/span>, util.plugin<\/span>.apiDoc<\/span>],\n    api<\/span>: {\n      \"post \/api\/login\"<\/span>: util.side<\/span>({\n        tags<\/span>: [`admin`<\/span>],\n        summary<\/span>: `\u6839\u636e\u7528\u6237\u540d\u83b7\u53d6 token`<\/span>,\n        schema<\/span>: {\n          body<\/span>: joi\n            .object<\/span>({\n              username<\/span>: joi\n                .string<\/span>()\n                .default<\/span>(`\u674e\u857e`<\/span>)\n                .required()\n                .description<\/span>(`\u7528\u6237\u540d`<\/span>),\n            })\n            .description<\/span>(`\u7528\u6237\u4fe1\u606f`<\/span>),\n        },\n        async<\/span> action<\/span>(req, res<\/span>) {\n          const<\/span> { username } = req.body<\/span>;\n          res.json<\/span>({\n            status<\/span>: 200<\/span>,\n            message<\/span>: `\u6b22\u8fce ${username}<\/span>, \u767b\u5f55\u6210\u529f`<\/span>,\n            token<\/span>: `tokentoken`<\/span>,\n          });\n        },\n      }),\n    },\n  };\n};\n<\/code><\/pre>\n

      \u67e5\u770b\u63a5\u53e3\u6587\u6863<\/h3>\n

      \u6d4f\u89c8\u5668\u6253\u5f00 http:\/\/127.0.0.1:9000\/doc<\/code> \u5373\u53ef<\/p>\n

      \"\u540e\u7aef\u95ee\uff1a\u4f5c\u4e3a\u524d\u7aef\u5982\u4f55\u5b9e\u73b0\u81ea\u52a8\u751f\u6210\u63a5\u53e3\u6587\u6863<\/p>\n

      \u4e00\u4e2a\u57fa\u4e8e express \u7684\u6846\u67b6\u3002\u5b83\u53ef\u4ee5\u5feb\u901f\u751f\u6210 api \u4ee5\u53ca\u521b\u9020\u6570\u636e\uff0c\u5f00\u7bb1\u5373\u7528\uff0c\u4fbf\u4e8e\u90e8\u7f72\u3002<\/p>\n

      github \u5730\u5740\uff1agithub.com\/wll8\/mockm\uff0c\u6c42 star \u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u540e\u7aef\u95ee\uff1a\u4f5c\u4e3a\u524d\u7aef\u5982\u4f55\u5b9e\u73b0\u81ea\u52a8\u751f\u6210\u63a5\u53e3\u6587\u6863\u4f5c\u4e3a\u4e00\u4e2a\u524d\u7aef\uff0c\u4e86\u89e3\u63a5\u53e3\u6587\u6863\u7684\u751f\u6210\u65b9\u5f0f\uff0c\u4ee5\u53ca\u5b9e\u73b0\u4e00\u4e2a\u63a5\u53e3\u6587\u6863\u81ea\u52a8\u751f\u6210\u3001\u63a5\u53e3\u6570\u636e...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[82],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/703"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=703"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/703\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}