当前位置: 首页 > 后端技术 > Node.js

使用apidoc文档神器快速生成api文档

时间:2023-04-03 10:59:57 Node.js

写好api接口之后,就需要写api文档了。如果手写一个一个写,会很麻烦。您必须使用apidoc才能仅通过编写注释来快速生成文档。安装的第一步是安装全局模块apidoc。npminstallapidoc-g修改界面注释在novel-api项目的routes下找到index.js文件,修改注释如下/***@api{get}/index请求首页数据*@apiVersion1.0.0*@apiName获取首页数据*@apiGroupindex***@apiSuccess{Number}flag是否获取数据1成功0失败*@apiSuccess{Array}books返回图书内容*@apiSuccess{String}msg返回信息**@apiSuccessExample{json}成功响应:*HTTP/1.1200OK*{*"flag":1,*"books":[*{*"_id":"5816b415b06d1d32157790b1",*"title":"HolyMarket",*"author":"Chendong",*"shortIntro":"废墟中崛起,灭亡复兴。大海化作尘土,雷电干涸,一缕雾气再次逼近大地,世界的枷锁被打开,一个全新世界的神秘角落被揭开...",*"cover”:“http://statics.zhuishushenqi.com/agent/http%3A%2F%2Fimg.1391.com%2Fapi%2Fv1%2Fbookcenter%2Fcover%2F1%2F1228859%2F1228859_fac7917a960547eb953edf0b740cef3a.jpg"%2F",*"site":"zhuishuviorFantasy",:",*"minorCate":"东方奇幻",*"allowMonthly":false,*"banned":0,*"latelyFollower":283375,*"retentionRatio":"73.42"*}*],*"msg":"OK"*}***@apiErrorExample错误响应:*HTTP/1.1404NotFound*{"flag":0,"msg":"rankingId有问题"}*/@api{method}path[title]@api如果没有@apidoc将忽略此注释methodmethodpathpathtitletitle@apiVersionversion设置文档块的版本。version版本号@apiNamename定义方法文档块的名称。该名称将在生成的输出中使用Subnavigation.name方法的名称@apiGroupname定义方法文档块属于哪个组。该组将用于生成的输出中的主导航。name组的名称。也用作导航标题。@apiSuccess[(group)][{type}]field[description]成功返回参数。(group)optional所有参数将按此名称分组。无组,默认成功200设置。{type}可选返回类型字段返回标识符描述描述@apiParamExample[{type}][title]example参数请求示例。{type}OptionalResponseformattitle示例的缩写example详细示例@apiErrorExample[{type}][title]example错误返回信息的示例,输出为预先格式化的代码。{type}可选响应格式title示例缩写示例详细示例配置npmrundoc打开package.json文件添加doc命令配置"doc":"apidoc-iroutes/-opublic/"routes/输出API文档文件夹。public/输出文件到公用文件夹,不是自动创建的。执行npmrundoc访问http://localhost:3000/查看生成的API文档。在线生成的文档地址是https://api.langpz.com/myblog和github,喜欢的话请点一下,谢谢。https://github.com/lanpangzhihttp://blog.langpz.com参考https://github.com/apidoc/apidoc