const Koa = require("koa");
const Router = require("koa-router");
const static = require("koa-static")
const views = require("koa-views");
let data = require("./data/data.json");
let app = new Koa();
let router = new Router();
// 参数1 : 指定加载路径 localhost:8000/views
// 参数2 : 配置指定的模板
app.use(views(__dirname+"/views",{
extension:"pug"
}));
app.use(static(__dirname+"/static"));
router.get("/",async ctx=>{
// ctx.body = "hello"
let perPage = 5;
let p = ctx.query.p || 1;
// 获取对应的数据
let newData = JSON.parse(JSON.stringify(data)).splice((p-1)*perPage,perPage);
let totalpage = Math.ceil(data.length / perPage);
await ctx.render("index",{
newData,
totalpage,
p
})
})
app.use(router.routes());
app.listen(8080);