The separation of the front and back ends requires RESTful interfaces to exchange data, and interface documents serve as Bridges between the front and back ends. Writing standard interface documentation and changing it as project requirements change is a pain point in back-end development. The introduction of the Hapi-Swagger module helps us solve this problem. The HApi-Swagger plug-in automatically generates standard API documentation as long as it is configured synchronously in the project code. The Hapi-Swagger plug-in provides a UI interface that is accessed through a browser, making it very easy to test the API.
Based on the author’s previous article “Building a RESTful CRUD API with HAPI and mongoDB in 5 Minutes”, this article introduces how to introduce the configuration of hapi-Swagger plug-in, according to the code automatically generate API interface document test interface.
The installationhapi-swagger
The plug-in
Hapi-swagger plugin needs to work with @hapi/ Inert, @HApi/Vision, @HApi/JoI, package plugin.
Joi is a validation module that verifies the compliance of data structures with numerical values.
Install the above plug-ins with the following command:
npm i api-swagger @hapi/inert @hapi/vision @hapi/joi package
Copy the code
The introduction of the plugin
Added api-swagger @hapi/inert @hapi/vision plugin to the entry file index.js.
const Inert = require('@hapi/inert');
const Vision = require('@hapi/vision');
const HapiSwagger = require('hapi-swagger');
const Pack = require('./package');
Copy the code
Register plugin in const init = async () => {} function:
const swaggerOptions = {
info: {
title: 'Test API Documentation',
version: Pack.version,
},
};
await server.register([
Inert,
Vision,
{
plugin: HapiSwagger,
options: swaggerOptions
}
]);
Copy the code
The use of plug-in
Configure this in the /routes/posts.js file.
First, we need to introduce joI:
const Joi = require('@hapi/joi')
Copy the code
Then, configure the Posts custom routing plug-in:
exports.plugin = {
name:'routePosts',
version: '1.0.0',
//pkg: require('.. /package.json'),
register: async function (server, options) {
server.route({
path: '/posts',
method: 'GET',
options: {
handler: PostsController.getAll,
description: 'get posts',
tags: ['api']}}); server.route({ path:'/posts/{id}',
method: 'GET',
options: {
handler: PostsController.findById,
description: 'get post by id',
tags: ['api'],
validate: {
params: Joi.object({
id: Joi.string()
.required()
.description('the id of the post')})}}}); server.route({ path:'/posts',
method: 'POST',
options: {
handler: PostsController.create,
description: 'create post',
tags: ['api'],
validate: {
payload: Joi.object({
title: Joi.string()
.required()
.description('the post title'),
content: Joi.string()
.description('the post content'),
rating: Joi.number()
.description('the post rating'),})}}}); server.route({ path:'/posts/{id}',
method: 'PATCH',
options: {
handler: PostsController.edit,
description: 'get post by id',
tags: ['api'],
validate: {
params: Joi.object({
id: Joi.string()
.required()
.description('the id of the post')
}),
payload: Joi.object({
title: Joi.string()
.required()
.description('the post title'),
content: Joi.string()
.required()
.description('the post content'),
rating: Joi.number()
.required()
.description('the post rating'),})}}}); server.route({ path:'/posts/{id}',
method: 'DELETE',
options: {
handler: PostsController.delete,
description: 'get post by id',
tags: ['api'],
validate: {
params: Joi.object({
id: Joi.string()
.required()
.description('the id of the post')})}}}); }Copy the code
Finally, register the custom routing plug-in in the entry file index.js:
await server.register([{
plugin: require('./routes/posts'),
options:{}
}
]);
Copy the code
test
Enter the command in the terminal window:
node .
Copy the code
After the project is successfully started, the following message is displayed:
Listening on http://localhost:8000
Copy the code
Type http://localhost:8000/documentation in the browser interface document interface through test.