Swagger UI教程 API 文档神器 搭配Node使用

环境搭建

下载Swagger UI(也可以直接下载 zip 文件)
{% highlight null %}

git clone https://github.com/swagger-api/swagger-ui.git
{% endhighlight %}
安装 express
创建一个空文件夹node_app

{% highlight null %}
mkdir node_app
{% endhighlight %}
初始化 node ,创建package.json文件()
{% highlight null %}
cd node_ap
npm init
// 下面的看你心情填写
name: (node_app) node_app
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author:
license: (ISC)
{% endhighlight %}
安装 express
{% highlight null %}
npm install express –save
{% endhighlight %}
创建 index.js
{% highlight null %}
vim index.js
{% endhighlight %}
把下面代码贴如 index.js 中
{% highlight null %}
var express = require(‘express’);
var app = express();
app.get(‘/’, function (req, res) {
res.send(‘Hello World!’);
});

app.listen(3000, function () {
  console.log('Example app listening on port 3000!');
});

{% endhighlight %}
在 node_app 中创建空目录 public
{% highlight null %}

  mkdir public
  cd public

{% endhighlight %}
修改路由
{% highlight null %}
vim ../index.js
//在文件第三行插入下面这句话
app.use(‘/static’, express.static(‘public’));
{% endhighlight %}
把下载好的Swagger UI 文件中dist 目录下的文件全部复制到 public 文件夹下。
如下图:

开启 node
{% highlight null %}
node index.js
{% endhighlight %}
打开浏览器,输入http://localhost:3000/static/index.html“>http://localhost:3000/static/index.html

到此为止,你已经把官方的 demo 在本地配置好了。当然你也可以吧这个搭建在服务器上

编写文档并发布

使用Swagger Editor编写 API 文档

Swagger Editor 上的是基于 yaml 的语法,但是不用害怕,看着官方的 demo 看个10分钟就会了。

导出 test.json 文档

把 test.json 放到 node_app/public 目录下。
利用编辑器修改 index.html文件

url = “http://petstore.swagger.io/v2/swagger.json“;

url = “/static/test.json”;

重启 node 服务,浏览器中打开http://localhost:3000/static/index.html就是你自己写的 api 文档了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值