feat: 路由遍历

This commit is contained in:
imsyy 2023-04-24 16:02:04 +08:00
parent b69c30d727
commit 04452a5fad

View File

@ -1,18 +1,7 @@
const fs = require("fs");
const path = require("path");
const Router = require("koa-router"); 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(); const router = new Router();
// 根目录 // 根目录
@ -20,18 +9,17 @@ router.get("/", async (ctx) => {
await ctx.render("index"); await ctx.render("index");
}); });
router.use(bilibiliRouter.routes()); // 遍历所有路由模块
router.use(zhihuRouter.routes()); fs.readdirSync(__dirname)
router.use(baiduRouter.routes()); .filter((filename) => filename.endsWith(".js") && filename !== "index.js")
router.use(weiboRouter.routes()); .forEach((filename) => {
router.use(itHomeRouter.routes()); const routerPath = path.join(__dirname, filename);
router.use(krRouter.routes()); const routerModule = require(routerPath);
router.use(sspaiRouter.routes()); // 自动注册路由
router.use(tiebaRouter.routes()); if (routerModule instanceof Router) {
router.use(toutiaoRouter.routes()); router.use(routerModule.routes());
router.use(thepaperRouter.routes()); }
router.use(juejinRouter.routes()); });
router.use(newsqqRouter.routes());
// 404 路由 // 404 路由
router.use(async (ctx) => { router.use(async (ctx) => {