首先得安装node
1,在定好路径下创建空文件夹node_app
mkdir node_app;
2,cd node_app
nom init(一直enter)
3,安装express
npm install express --save
4,创建index.js
vim index.js
5,把代码贴到index.js中
var app = express();
app.get('/', function (req, res) {
res.send('Hello World!');});
app.listen(3000, function () {
console.log('Example app listening on port 3000!');
});
7,git clone https://github.com/swagger-api/swagger-ui.git
8,将下载的swagger-ui中的dist文件夹拷贝到public目录中
9,在index.js中加入
app.use('/static', express.static('public'));
10,node index.js启动node服务
11,输入http://localhost:3000/static /dist/index.html即可看到接口文档
,二 ,后台启动node
安装 npm install forever -g
forever start index.js就可以了,如果forever命令找不到,记得做个软连接 ln -s forever路径 、usr/local/bin/forever
三,实现不同路径展示不同文档
在public下创建resources,将css和js文件放到resources中,在public下创建index文件夹,将index.html放入到index文件夹下,并将index。html中的资源路径改为../resources/xxxx相对路径,这样访问index.html的时候即http://localhost:3000/static/index;同理创建每个项目的文件夹比如clp,并把index中的index.html复制到项目clp的文件夹下,把clp项目生成的json文件传到对应的clp项目文件夹下,并把index.html中的url改为:http://localhost:3000/static/clp/swagger.json. 然后浏览器输入:http://localhost:3000/static/clp/index.html ,ok;
四,自动化实现Jenkins打包的json文件传到另一个服务器,并展示在swagger-ui服务上
在Jenkins的excude shell中
ssh -p 22222 $SWAGGER_URL rm -rf $PUBLIC_ROOT/$JOB_NAME
ssh -p 22222 $SWAGGER_URL mkdir $PUBLIC_ROOT/$JOB_NAME
scp -P 22222 $DEST/api/swagger.json $SWAGGER_URL:$PUBLIC_ROOT/$JOB_NAME/
ssh -p 22222 $SWAGGER_URL cp $INDEX_FILE $PUBLIC_ROOT/$JOB_NAME/
ssh -p 22222 $SWAGGER_URL sh /nodeservice/node_app/replace_str.sh $JOB_NAME
$SWAGGER_URL:目标主机
$PUBLIC_ROOT/$JOB_NAME:目标主机路径
$DEST/api/swagger.json:存放json文件主机路径
$DEST/api/swagger.json :项目名
replace_str.sh:
#! /bin/sh
NAME=$1
exp="'s|http://petstore.swagger.io/v2/swagger.json|http://xxx:3000/$NAME/swagger.json|g'"
sed_cmd="sed -i $exp $PUBLIC_ROOT/$NAME/index.html"
eval $sed_cmd