hapi framework construction record (2): routing transformation and generating interface documents

Time:2022-8-13

hapi framework construction record (2): routing transformation and generating interface documents

  • hapi framework, withOfficial websiteThe brief is: Hapi is a rich framework for building applications and services that enables developers to focus on writing reusable application logic instead of spending time building infrastructure.
  • In my own words, it is a basic framework for node services like express and koa.
  • This blog is after reading the Nuggets booklet"Hapi-based Node.js applet back-end development practice guide"And after practice, record the practice process and the pits stepped on. Interested readers can support reading the original content of the Nuggets Booklet.

route summary

1. Create a new index.js in the ./routes directory

As a summary of routes, just create a new file under ./routes in the future

"use strict";

const fs = require("fs");
const path = require("path");
const basename = path.basename(__filename); // current filename
let routeArr = [];

// Synchronously read the current directory and filter the filename array except the current file
fs.readdirSync(__dirname)
  .filter(file => {
    // Filter out hidden files, current files, non-js files, and return an array of file names in the current directory
    return (
      file.indexOf(".") !== 0 && file !== basename && file.slice(-3) === ".js"
    );
  })
  .forEach(file => {
    // import routing module
    let arr = require(path.join(__dirname, file));
    // summary
    routeArr.push(...arr);
  });

module.exports = routeArr;

test route

1. Create a new test.js file as a test

const GROUP_NAME = "test";

module.exports = [
    // pure test return interface
  {
    method: "GET",
    path: `/${GROUP_NAME}`,
    handler: (request, h) => {
      const data = { message: "test" };

      // Response data method:
      // return h.response(data).code(200);
      return data;
    }
  },
]

2. Modify app.js

hapi framework construction record (2): routing transformation and generating interface documents

3. Browse the test results

hapi framework construction record (2): routing transformation and generating interface documents

Interface Documentation Swagger

1. Understandswagger

This hapi plug-in is a powerful and easy-to-use tool. It can generate corresponding interface documents, test interface data, and cooperate with Joi plug-in to verify data according to the config configuration given by the route.

2. Install swagger related dependencies

npm i hapi-swagger inert vision package

3. Write a configuration plugin

Create a new hapi-swagger.js in the ./plugins directory

Can refer toOfficial example

const hapiSwagger = require("hapi-swagger");
const inert = require("inert");
const vision = require("vision");
const package = require("package");

module.exports = [
  inert,
  vision,
  {
    plugin: hapiSwagger,
    options: {
      info: {
        title: "Interface Documentation", // the title of the documentation
        version: package.version // version of the documentation
      },
      // Define the interface with the tags attribute as the category [define the title of the category], and use it for the configuration config:tags of the ./routes route
      grouping: "tags",
      // Labels, used to categorize the tags defined by the routing config
      tags: [
        { name: "test", description: "Test related" }
      ],
      lang: 'zh-cn' // Documentation language
    }
  }
];

4. The app.js entry file configures the swagger plugin

hapi framework construction record (2): routing transformation and generating interface documents

5. Access interface documentation

address:http://localhost:3000/documentation

hapi framework construction record (2): routing transformation and generating interface documents

6. Routing configuration production test interface

Modify the test route file ./routes/test.js

hapi framework construction record (2): routing transformation and generating interface documents

Visit the document address again

hapi framework construction record (2): routing transformation and generating interface documents