require-directory的安装与使用
const Koa = require('koa');
const app = new Koa();
const requireDirectory = require('require-directory');
const Router = require('koa-router');
const modules = requireDirectory(module,'./api',{
visit:whenLoadModule
})
function whenLoadModule(obj){
if(obj instanceof Router){
app.use(obj.routes())
}
}
app.listen(3000);
路由文件的写法api/v1/book.js
const Router = require('koa-router');
const router = new Router();
router.get('/v1/book/latest',(ctx,next)=>{
ctx.body={key:'books'}
})
module.exports = router
目录结构
喜欢这篇文章的话 打赏一下吧!