一起来完成以下步骤:
- 创建一个简单的Router
- 分别创建user(以user开头的url)和admin(以admin开头的url) 的Router
- 实现动态uri的Router
#main.ts
import { Application, Router } from "https://deno.land/x/oak/mod.ts";
import UserRouter from "./user-router.ts";
import AdminRouter from "./admin-router.ts";
const app = new Application();
const router = new Router();
router.get("/",(ctx) =>{
ctx.response.body="Hello World";
})
app.use(router.routes());
app.use(router.allowedMethods());
app.use(UserRouter.routes());
app.use(UserRouter.allowedMethods());
app.use(AdminRouter.routes());
app.use(AdminRouter.allowedMethods());
await app.listen({ port:8000 });
#admin-router.ts
import { Router } from "https://deno.land/x/oak/mod.ts";
const AdminRouter = new Router({prefix:"/admin"}); //以admin开头的uri
AdminRouter.get("/",(ctx) =>{
ctx.response.body ="Hello Admin";
})
export default AdminRouter;
#user-router.ts
import { Router } from "https://deno.land/x/oak/mod.ts";
const UserRouter = new Router({prefix:"/user"}); //以user开头的uri
UserRouter.get("/",(ctx) =>{
ctx.response.body ="Hello User";
}).get("/:id",(ctx) =>{
ctx.response.body = "the user is is "+ ctx.params.id;
})
export default UserRouter;
完
本文使用 mdnice 排版