1.0.6 • Published 5 years ago
small-swagger v1.0.6
small
small框架构建
joi 版本最好使用 ^14.3.1
swagger-client 版本最好使用 ^2.2.21
项目目录结构
application
apis
admin
api.js
controllers
admin
controller.js
middleware
xxx.js
xxx.js
首页引入 index.js
import small from "small-swagger"
import {join} from "path"
import joi from "joi"
small.use(中间件)
//这个是启动small程序
options = {
controllerDir: join(__dirname,'controller'),
apiDir:join(__dirname,"/apis"),
middlewareDir:[join(__dirname,"/middlewares")],
port:3000,
swagger:{
swagger: "2.0",
info: {
title: "接口文档",
description: "swagger 2.5.5",
version: "1.0.0"
},
basePath: "/v1",
schemes: ["http", "https"],
produces: ["application/json"],
}
*/
*/
*/
}
swagger地址是 basePath路径 /document
*/
small.loadServer(options).then(function(){
console.log('项目启动');
})
*/
api页面
export default [
{
path: '/brand',
method: 'get',
operationId: 'createBrand',
tags: ['brand'],
summary: '新增品牌',
description:"新增品牌哈哈哈",
handler: 'admin.brand.list',
request: {
params: {
id: Joi.number().integer().description('弹框信息id')
}
query: {
status: Joi.number().integer().valid(0, 1).description('启用状态,0-停用,1-已启用')
}
body:joi.object({
name: Joi.string().description('名称'),
image: Joi.string().description('图片'),
page_url: Joi.string().description('页面路径')
})
},
responses: {
schema: Joi.object({
msg: Joi.string().description('成功返回')
})
}
}
]
controller 页面
export default {
init() {
this.mixin({
async haha(){
console.log("哈哈控制函数");
},
afterConsole(){
}
})
this.before({name:"haha"});
this.after({name:"afterConsole"})
},
actions:{
getBrand:async function(ctx){
return {msg:"我是getBrand的控制器"}
},
list:async function(ctx){
return {
code:302,
render:async function(){
//ctx.render需增加引入模板引擎中间件
await ctx.render("index")
}
}
}
}
};