How to Create Router in Oak in Deno: Simple Guide
To create a router in Oak for Deno, import
Router from "https://deno.land/x/oak/mod.ts", then create a new router instance with new Router(). Define routes using methods like router.get() and attach the router's routes to the Oak application with app.use(router.routes()).Syntax
The basic syntax to create a router in Oak involves importing Router, creating an instance, defining routes, and using the router in your app.
import { Router } from "https://deno.land/x/oak/mod.ts";- imports the Router class.const router = new Router();- creates a new router instance.router.get(path, handler)- defines a GET route with a path and handler function.app.use(router.routes())- adds the router's routes to the Oak application.
typescript
import { Router } from "https://deno.land/x/oak/mod.ts"; const router = new Router(); router.get("/", (context) => { context.response.body = "Hello from Oak router!"; }); // Later in your app setup // app.use(router.routes());
Example
This example shows a complete Oak server with a router that handles two routes: the root / and /about. It demonstrates how to create the router, define routes, and start the server.
typescript
import { Application, Router } from "https://deno.land/x/oak/mod.ts"; const router = new Router(); router .get("/", (context) => { context.response.body = "Welcome to the home page!"; }) .get("/about", (context) => { context.response.body = "About us page."; }); const app = new Application(); app.use(router.routes()); app.use(router.allowedMethods()); console.log("Server running on http://localhost:8000"); await app.listen({ port: 8000 });
Output
Server running on http://localhost:8000
Common Pitfalls
Common mistakes when creating routers in Oak include:
- Forgetting to call
app.use(router.routes()), so routes never get registered. - Not calling
router.allowedMethods(), which helps handle HTTP method responses properly. - Defining routes after starting the server, which means new routes won't be recognized.
- Using incorrect path strings or handler signatures.
typescript
/* Wrong: Missing router.routes() */ import { Application, Router } from "https://deno.land/x/oak/mod.ts"; const app = new Application(); const router = new Router(); router.get("/", (ctx) => { ctx.response.body = "Hi"; }); // app.use(router.routes()); // <-- Missing this line await app.listen({ port: 8000 }); /* Right: Include router.routes() and allowedMethods() */ app.use(router.routes()); app.use(router.allowedMethods());
Quick Reference
| Method | Description |
|---|---|
| Router() | Creates a new router instance |
| router.get(path, handler) | Defines a GET route |
| router.post(path, handler) | Defines a POST route |
| app.use(router.routes()) | Adds router routes to the app |
| app.use(router.allowedMethods()) | Handles HTTP method responses |
Key Takeaways
Import Router from Oak and create a new instance with new Router().
Define routes using router.get(), router.post(), etc., with path and handler.
Attach router routes to the app using app.use(router.routes()) and app.use(router.allowedMethods()).
Always call router.allowedMethods() to handle HTTP method responses correctly.
Define all routes before starting the server to ensure they are registered.