DailyHotApi/routes/index.js

30 lines
685 B
JavaScript
Raw Normal View History

2023-04-24 08:02:04 +00:00
const fs = require("fs");
const path = require("path");
2023-03-14 08:04:10 +00:00
const Router = require("koa-router");
const router = new Router();
// 根目录
router.get("/", async (ctx) => {
await ctx.render("index");
});
2023-04-24 08:02:04 +00:00
// 遍历所有路由模块
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());
}
});
2023-03-14 08:04:10 +00:00
// 404 路由
router.use(async (ctx) => {
await ctx.render("404");
});
module.exports = router;