Skip to content

Koa 自动添加路由模块,项目启动,自动注册路由!

Notifications You must be signed in to change notification settings

AdleyTales/koa-dynamic-router

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

koa-dynamic-router

Koa 自动添加路由模块,项目启动,自动注册路由!

这样在routes目录下添加子路由,就会在项目重新启动加载时,自动挂载到根路由上

核心代码

const Router = require('koa-router');
const router = new Router();

const fs = require('fs');
const path = require('path');

const files = fs.readdirSync(__dirname);

files
    .filter(file => file.endsWith('.js'))
    .forEach(file => {
        const file_name = file.substr(0, file.length - 3);
        const file_entity = require(path.join(__dirname, file));
        if (file_name !== 'index') {
            router.use(`/${file_name}`, file_entity.routes(), file_entity.allowedMethods())
        }
    });

module.exports = router;

About

Koa 自动添加路由模块,项目启动,自动注册路由!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published