视频讲解
https://www.bilibili.com/vide…
一起来完成以下步骤:
- 创建一个简单的 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;
完