diff --git a/routes/index.js b/routes/index.js index 263b5ff..0b58d68 100644 --- a/routes/index.js +++ b/routes/index.js @@ -1,18 +1,7 @@ +const fs = require("fs"); +const path = require("path"); const Router = require("koa-router"); -const bilibiliRouter = require("./bilibili"); -const zhihuRouter = require("./zhihu"); -const baiduRouter = require("./baidu"); -const weiboRouter = require("./weibo"); -const itHomeRouter = require("./ithome"); -const krRouter = require("./36kr"); -const sspaiRouter = require("./sspai"); -const tiebaRouter = require("./tieba"); -const toutiaoRouter = require("./toutiao"); -const thepaperRouter = require("./thepaper"); -const juejinRouter = require("./juejin"); -const newsqqRouter = require("./newsqq"); - const router = new Router(); // 根目录 @@ -20,18 +9,17 @@ router.get("/", async (ctx) => { await ctx.render("index"); }); -router.use(bilibiliRouter.routes()); -router.use(zhihuRouter.routes()); -router.use(baiduRouter.routes()); -router.use(weiboRouter.routes()); -router.use(itHomeRouter.routes()); -router.use(krRouter.routes()); -router.use(sspaiRouter.routes()); -router.use(tiebaRouter.routes()); -router.use(toutiaoRouter.routes()); -router.use(thepaperRouter.routes()); -router.use(juejinRouter.routes()); -router.use(newsqqRouter.routes()); +// 遍历所有路由模块 +fs.readdirSync(__dirname) + .filter((filename) => filename.endsWith(".js") && filename !== "index.js") + .forEach((filename) => { + const routerPath = path.join(__dirname, filename); + const routerModule = require(routerPath); + // 自动注册路由 + if (routerModule instanceof Router) { + router.use(routerModule.routes()); + } + }); // 404 路由 router.use(async (ctx) => {