preface

  • Online music poke me ah!
  • Music blog source code online!
  • Decorator koa-Swagger-decorator decorator decorator decorator decorator decorator decorator

Looking for information

Because see the official website example, after the error:

I started to try Baidu, Google search, gold digging, and even Tencent Class, MOOC and Group, but still got no response. Just when I was about to give up, I found a very late article in Baidu search, and saw that Git has a server scaffolding called KOA2-Swagger. Decorator: koa-swagger-decorator: Decorator: koa-swagger-decorator: koa-swagger-decorator

Start Handle

install

npm i koa-swagger-decorator --save-dev
Copy the code

New file swaggerDec.js

const { SwaggerRouter } = require('koa-swagger-decorator');
const path = require('path')
const router = new SwaggerRouter();

// swagger docs avaliable at http://localhost:3333/swagger-html
router.swagger({
    title: 'Scheduling System'.description: 'API DOC'.version: '1.0.0'
});
// Find the routes folder in the corresponding directory
router.mapDir(path.resolve(__dirname, '.. /routes/'));

module.exports = router
Copy the code

Introduced in app.js

require("babel-register");
const swaggerDec = require('./config/swaggerDec');

app.use(swaggerDec.routes(), swaggerDec.allowedMethods())
Copy the code

Note that you need to introduce babel-Register to support es7 decorator syntax. For details on how to install and use it, please step by step to teach Koa2 how to write interface documentation (not writing interface documentation is not a good backend).

Then I test and create a new test.js

Then expertly introduce test.js in app.js

require("babel-register");
const swaggerDec = require('./config/swaggerDec');
const test = require('./routes/test') 

app.use(swaggerDec.routes(), swaggerDec.allowedMethods())
app.use(test.routes(), test.allowedMethods())
Copy the code

Let’s see if we can show ourswagger

Visit: http://localhost:3333/swagger-html

As expected, you should see the GET /featuresDev/ interface we defined

Well, it can be seen that Swagger has been displayed normally. Although the interface does not come out, I am very relieved that there will be no error after all, so I think there may be something wrong with the path.

Check.

Our interface is written in the Routes folder.

Resolve (__dirname, ‘.. /routes/’))

After a very long process of screening, checking and searching, the final reason is that the syntax is wrong. (Just know)

First of all, our swaggerDec.js is written correctly!

Second, app.js does not need to be imported into test.js

Why is that?

Since our swaggerDes. Js has scanned the interface under routes folder, there is no need to introduce it separately.

The test.js file only needs to export the class

What do you mean?

Write the interface and decorator (interface documentation) in the methods of the class.

Add delete change check above, see effect.

It was really not easy, finally the interface document to understand.

Make a day, mountain poor water answer doubt no road, a village ah!

Now I can clean up the interface documentation of my music blog, Year!

Code source please poke a poke!

Tips – Babel

As you can see, the koa-Swagger-decorator uses the decorator syntax, so we need to install it using Babel

Install Babel

npm i babel-cli babel-core babel-plugin-transform-decorators-legacy babel-register --save-dev
Copy the code

Create a new.babelrc file

{
    "presets": []."plugins": [
        "transform-decorators-legacy"]}Copy the code

Upon successful configuration, the decorator syntax is ready to use.

The last

Why do I have to look into the koa-Swagger-jsdoc decorator, because there are so many annotations written in swagger-JsDoc that the entire file will be filled with them when opened, for interface documentation, so using the annotations decorator will improve code visibility!

Of course, when we use a thing, it is impossible to use and write it all at once. Don’t be afraid of difficulties. The terrible thing is that you haven’t done it and you can’t do anything about it. When I was about to give up, I finally found a solution.

This is the words on the wall when I joined the Youth League committee:

Stick to the powerless, struggle to move their own.

And to all of you.

Seems to write a lot of irrelevant technical words, ha ha ha, maybe I am really feeling, too excited!

trivia

If you’re tired, guys, let me show you something interesting

Hot pursuit ah, I said I literally write, but also crazy pull me cross-examine.

Refer to the article

Koa2-swagger server scaffolding

In the past to recommend

Step by step to teach Koa2 how to write interface documentation (not writing interface documentation is not a good back-end)

Vue-cli3 builds the component library

Vue implements dynamic routing (and the interviewer blows project highlights)

Axios manipulation in a project you didn’t know about (Core principles of handwriting, compatibility)

VuePress builds project component documentation

Vue koa2 + + nginx deployment

Vue-typescript-admin-template background management system

The original link

Juejin. Cn/post / 694944…