Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

【模范代码】路由书写

路由

  • 文件开头

    import config from "config";
    import { Log } from "../../../utils/api_log.js";
    const log = new Log("index");
    
    import KoaRouter from "koa-router";
    const router = new KoaRouter({
        prefix: config.get("routerPrefix") + "/v1"
    });
    
  • 文件结尾

    export default router;
    
  • 单个路由(返回json)

    router.get("/", async ctx => {
        log.debug("== ");
        
        ctx.status = 200;
        ctx.body = {
        
        }
    })