当前位置:网站首页>Swagger UI tutorial API document artifact

Swagger UI tutorial API document artifact

2022-07-06 20:56:00 51CTO

Catalog

[TOC]

Preface

In some interface projects ,API Is used frequently , So a API Online document generation and testing tools are essential . and Swagger UI It's such a practical online tool
This blog describes how to follow the Swagger UI, Note that the company's test server is Linux Systematic , Therefore, this blog only introduces based on Linux Systematic Swagger Environment building process

Server environment installation ##

NodeJS Download deployment ###

First you need to download NodeJS, To nodejs Official website ​ ​http://nodejs.org/dist/v0.10.26​​​, download node-v0.10.26-linux-x64.tar.gz, Because this is already compiled , So we first use software to upload files to Linux The server , have access to WinSCP
And then use Linux Of cd The command comes to you nodejs Location of compressed files
The decompression command is as follows :

      
      
tar xvf node-v0.10.26-linux-x64.tar.gz
  • 1.

And then go to the file

      
      
cd node-v0.10.26-linux-x64/bin
  • 1.

perform

      
      
./node
  • 1.

Set up node overall situation

      
      
ln -s /root/Swagger/node-v0.10.26-linux-x64/bin/node /usr/local/bin/node
  • 1.

Set up npm overall situation

      
      
ln -s /root/Swagger/node-v0.10.26-linux-x64/bin/npm /usr/local/bin/npm
  • 1.

testing nodejs and npm Is the installation successful

      
      
node -v
npm -v
  • 1.
  • 2.

You can see the version number , What installation succeeded

express Download deployment ####

Install well NodeJS and NPM after , We can install it express 了

      
      
npm install express --save
  • 1.

The following important modules are needed with express The frame is installed together :
body-parser - node.js middleware , Used for processing JSON, Raw, Text and URL Encoded data .
cookie-parser - This is an analysis Cookie Tools for . adopt req.cookies You can take it from cookie, And turn them into objects .
multer - node.js middleware , Used for processing enctype="multipart/form-data"( Set the MIME code ) Form data for .

      
      
npm install body-parser --save
npm install cookie-parser --save
npm install multer --save
  • 1.
  • 2.
  • 3.

Check whether the installation is successful

      
      
npm list express
  • 1.

NodeJS Boot up ####

Then set the NodeJS Boot up
install forever

      
      
npm install forever -g
  • 1.

Set up

      
      
forever start test.js
forever stop test.js
forever restart test.js
  • 1.
  • 2.
  • 3.

Swagger UI Installation and deployment ##

github On clone:​ ​https://github.com/swagger-api/swagger-ui​​ If there is a download Git, Just use git download , Get access to github, Direct download

Then, according to the needs of the company , stay Linux On mkdir, I was there first root route , Create a node_app file

      
      
mkdir node_app
  • 1.

Get into node_app

      
      
cd node_app
  • 1.

Environment configuration

      
      
npm init
  • 1.

The following tips can be filled in according to your own needs

      
      
name: (node_app) node_app
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

VI Editor use :

      
      
vim index.js
  • 1.

Add code

      
      
var express = require('express');
var app = express();

app.get('/', function (req, res) {
res.send('Hello World');
})

var server = app.listen(3000, function () {

var host = server.address().address
var port = server.address().port

console.log(" Application example , The visiting address is http://%s:%s", host, port)

})
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

Enable server

      
      
node index.js
  • 1.

visit , Input http:// Company servers IP:3000
You can see Helloworld, Of course, we need to deploy Swagger
stay node_app So let's create one public Folder

      
      
mkdir public
cd public
  • 1.
  • 2.

Then download to swagger ui Inside dist Copy the files in the folder to public Folder

modify index.js

      
      
var express = require('express');
var app = express();

app.use('/swagger',express.static('public'));

app.get('/', function (req, res) {
res.send('Hello World');
})

var server = app.listen(3000, function () {

var host = server.address().address
var port = server.address().port

console.log("swagger start", host, port)

})
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

Then input ​ ​http://ip:3000/swapper​​, Yes, you can see something similar page

Swagger UI course API Document artifact _github

Picture description here

Swagger Editor Use ##

Swagger Editor yes Swagger UI The online editor , We can build our own , You can also use the official , The official URL:​ ​http://editor.swagger.io/#/​

github On Clone:​ ​https://github.com/swagger-api/swagger-editor​​ After the swagger editor Put the document in the company Linux Server

Http server install ###

      
      
npm install -g http-server
  • 1.

Start the project http-server swagger-editor, The default is 8080 port
Set up port And start the swagger editor

      
      
http-server –p 2017 swagger-editor
  • 1.

Input ​ ​http://ip:2017/#/​​ You can access your own Swagger Editor Editor

Reference tutorial
​​ ​http://www.linuxidc.com/Linux/2014-07/104489.htm​​​ ​​

 ​http://www.linuxidc.com/Linux/2014-06/103372.htm​​​


原网站

版权声明
本文为[51CTO]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061233058053.html

随机推荐