• fastify-swagger 7.4.1


    在实际工作中,我们可以通过swagger插件将后台定义的路由通过对应的模式自动生成接口文档,同时可以直接在项目启动后浏览。

    一、安装与注册以及简单使用

    1.安装 npm  i @fastify/swagger@版本号

    通过上述命令安装指定版本的swagger插件,默认最新,值得注意的是swagger插件7.4.1版本和以上版本用法区别很大。我们先进行讲解7版本的

    2.接下来在plugin目录下,创建 swagger模块,代码如下(需要在路由插件注册好之前注册,可以在插件plugin文件夹中定义。)注意uiConfiig和exportRouter在swagger属性外边,我已经上两次当了。....无奈

    1. 'use strict'
    2. const fp = require('fastify-plugin')
    3. module.exports = fp(async function (fastify, octs) {
    4. fastify.register(require('@fastify/swagger'), {
    5. routePrefix: '/api/v1/docs',//接口文档的访问地址
    6. swagger: {
    7. info: {
    8. title: '图书管理系统接口文档',
    9. description: '基于fastify框架开发的后端接口项目',
    10. version: '0.1.0'
    11. },
    12. // externalDocs: {
    13. // url: 'https://swagger.io',
    14. // description: 'Find more info here'
    15. // },这部分是其他依赖
    16. host: 'localhost:3000',//接口访问地址的主机部分
    17. schemes: ['http'],//指定协议
    18. consumes: ['application/json'],
    19. produces: ['application/json'],
    20. securityDefinitions: {//鉴权定义
    21. apiKey: {
    22. type: 'apiKey',
    23. name: 'Authorization',
    24. in: 'header'
    25. }
    26. },
    27. security: [//开启鉴权
    28. {
    29. apiKey: [],
    30. }
    31. ],
    32. },
    33. uiConfig: {
    34. deepLinking: false
    35. },
    36. exposeRoute: true
    37. }
    38. )
    39. });

    3.最后修改app.js文件(新增swagger代码部分)

    1. 'use strict'
    2. const path = require('path')
    3. const AutoLoad = require('@fastify/autoload')
    4. // Pass --options via CLI arguments in command to enable these options.
    5. module.exports.options = {}
    6. module.exports = async function (fastify, opts) {
    7. // Place here your custom code!
    8. // Do not touch the following lines
    9. // This loads all plugins defined in plugins
    10. // those should be support plugins that are reused
    11. // through your application
    12. fastify.register(AutoLoad, {
    13. dir: path.join(__dirname, 'plugins'),
    14. options: Object.assign({}, opts)
    15. })
    16. // This loads all plugins defined in routes
    17. // define your routes in one of these
    18. fastify.register(AutoLoad, {
    19. dir: path.join(__dirname, 'routes'),
    20. options: Object.assign({prefix:'/api/v1'}, opts)
    21. }),
    22. //使用ready方法监听 项目已经准备好启动了
    23. fastify.ready().then(()=>{
    24. fastify.swagger();//生成文档(出必须在路由完成后)
    25. })
    26. }

    4.这样我们的一个简单的swagger引用就成功了。

     

    8以上的版本需要引入swagger-ui。(需要注意)以后用到8再进行复习。 

    二、基本用法

    第一大块schema

     

    1.在我们注册的路由user中指定标题、描述、补充描述(在get方法中的schema中的属性tag、、summar、descriptiony)

     

     2.查询参数querystring

    这样就可以通过查询参数获取数据了(try it out),但是此时并不会显示出数据(还需要响应)

     3.配置响应 (reponse)

     此时会响应我们定义好的reponse,如果并没有数据(没有进行Cors跨域,),前边的域名头换成localhost就好了

     

     

  • 相关阅读:
    HTTP请求和响应(补充HTTP协议)
    助力汽修企业突破转型瓶颈,S2B2B商城价格管理模块实现采购交易数字化升级
    自制代码编辑器:CASM Editor
    【AutoAugment】《AutoAugment:Learning Augmentation Policies from Data》
    head first java 读书笔记 2
    Antivirus Zap Pro :苹果 mac 电脑全面的系统安全解决方案
    提高应用程序测试覆盖率的 4 个步骤
    极简工作流「GitHub 热点速览」
    LeetCode每日一题(1325. Delete Leaves With a Given Value)
    flutter开发实战-StreamBuilder使用介绍及实例
  • 原文地址:https://blog.csdn.net/m0_72694993/article/details/127609314