前言最近在考虑整合服务器资源。作为一个多端调用的API,看到了Restful标准和ORM,但是找了很多版本写路由很麻烦,自己折腾了半天。考虑到API库的结构在顶层,对象名会越来越长,而且不容易维护。因此,在工作流模块中采用了原型、实例等部分层次结构。图层的深度定义为该图层的可访问对象集合(collection)的属性。满足Restful设计--工作流(类别)--原型(集合)--[方法]...--[方法]...--实例(集合)--用户(集合)--[方法]List#get:object/--[method]Instance#get:object/:id--...--...RESTFULAPI接口规范了RestfulAPI接口的命名。get('/',ctx=>{ctx.error('路径匹配失败')}).get('/:object',RestfulAPIMethods.List).get('/:object/:id',RestfulAPIMethods.Get).post('/:object',RestfulAPIMethods.Post).put('/:object/:id',RestfulAPIMethods.Replace).patch('/:object/:id',RestfulAPIMethods.Patch).delete('/:object/:id',RestfulAPIMethods.Delete).get('/:object/:id/:related',RestfulAPIMethods.Related).post('/:object/:id/:related',RestfulAPIMethods.AddRelated).delete('/:对象/:id/:相关/:relatedId',RestfulAPIMethods.DelRelated)APIobject该文件来自微信小程序demo。使用方便,放在需要引用的根目录下,直接获取文件目录结构API对象const_=require('lodash')constfs=require('fs')constpath=require('path')/***将d文件夹下的文件映射为模块*/constmapDir=d=>{consttree={}//获取当前文件夹下的所有文件夹和文件const[dirs,files]=_(fs.readdirSync(d)).partition(p=>fs.statSync(path.join(d,p)).isDirectory())//映射文件夹dirs.forEach(dir=>{tree[dir]=mapDir(path.join(d,dir))})//映射文件files.forEach(file=>{if(path.extname(file)==='.js'){tree[path.basename(file,'.js')]=require(path.join(d,file))tree[path.basename(file,'.js'))].isCollection=true}})returntree}//默认情况下,映射下currentfolderisexportedmodule.exports=mapDir(path.join(__dirname))koa-router分层路由的实现创建多层路由及其传递关系执行顺序为1--路径匹配--匹配到'结尾/'--匹配到对应的RestfulAPI执行结束--continue2--调用中间件Nest3--下一级路由4--循环到1constDefinedRouterDepth=2letrouters=[]for(leti=0;i
