Routing patterns help your web app decide what to do when someone visits different web addresses. They connect URLs to the right code that shows pages or handles actions.
Routing patterns in Deno
import { Router } from "https://deno.land/x/oak/mod.ts"; const router = new Router(); router.get("/path", (context) => { context.response.body = "Response for GET /path"; }); router.post("/path", (context) => { context.response.body = "Response for POST /path"; });
Use router.get, router.post, etc., to handle different HTTP methods.
The first argument is the URL pattern, the second is a function that runs when the URL matches.
/ and sends back "Home page".router.get("/", (ctx) => { ctx.response.body = "Home page"; });
:id to capture part of the URL and use it in the response.router.get("/users/:id", (ctx) => { const userId = ctx.params.id; ctx.response.body = `User ID is ${userId}`; });
/submit, often used for forms.router.post("/submit", (ctx) => { ctx.response.body = "Form submitted!"; });
This simple Deno app uses Oak router to handle three routes: home, about, and a dynamic user greeting. It listens on port 8000.
import { Application, Router } from "https://deno.land/x/oak/mod.ts"; const router = new Router(); router.get("/", (ctx) => { ctx.response.body = "Welcome to the home page!"; }); router.get("/about", (ctx) => { ctx.response.body = "About us page."; }); router.get("/users/:name", (ctx) => { const name = ctx.params.name; ctx.response.body = `Hello, ${name}!`; }); 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 });
Dynamic routes like /users/:name let you capture parts of the URL as variables.
Always add router.allowedMethods() to handle HTTP method responses properly.
Use app.use(router.routes()) to connect the router to your app.
Routing patterns connect URLs to code that runs when those URLs are visited.
Use static and dynamic routes to handle different pages and data.
Deno with Oak makes routing simple and organized for web apps.
Practice
Solution
Step 1: Understand routing role
Routing connects web addresses (URLs) to the code that should run when those addresses are accessed.Step 2: Compare options
Only To connect URLs to specific code that runs when those URLs are visited describes this connection. Other options describe unrelated tasks like styling or data storage.Final Answer:
To connect URLs to specific code that runs when those URLs are visited -> Option CQuick Check:
Routing = URL to code connection [OK]
- Confusing routing with styling or database tasks
- Thinking routing only manages user login
- Believing routing changes page colors
Solution
Step 1: Recall Oak routing syntax
Oak uses router.get(path, handler) to define GET routes. The handler sets ctx.response.body for response content.Step 2: Check each option
router.get('/home', (ctx) => { ctx.response.body = 'Welcome'; }); matches correct syntax. router.route('/home', (ctx) => { ctx.body = 'Welcome'; }); uses non-existent method 'route'. router.add('/home', (ctx) => { ctx.response.body = 'Welcome'; }); uses 'add' which is invalid. router.get('/home', (ctx) => { return 'Welcome'; }); returns string instead of setting ctx.response.body.Final Answer:
router.get('/home', (ctx) => { ctx.response.body = 'Welcome'; }); -> Option AQuick Check:
Oak static route uses router.get and ctx.response.body [OK]
- Using wrong router method like route or add
- Returning string instead of setting ctx.response.body
- Forgetting to set response body
router.get('/user/:id', (ctx) => {
const id = ctx.params.id;
ctx.response.body = `User ID is ${id}`;
});
// Request URL: /user/42What will be the response body when visiting
/user/42?Solution
Step 1: Understand dynamic route parameter
The route uses ':id' as a placeholder. When URL is '/user/42', ctx.params.id becomes '42'.Step 2: Check response body construction
The response body uses template string to insert id value, so it becomes 'User ID is 42'.Final Answer:
"User ID is 42" -> Option BQuick Check:
Dynamic param ':id' = '42' in response [OK]
- Using literal ':id' instead of param value
- Forgetting to access ctx.params.id
- Assuming undefined if param missing
router.get('/post/:postId', (ctx) => {
const postId = ctx.request.params.postId;
ctx.response.body = `Post: ${postId}`;
});Solution
Step 1: Check how to access route params in Oak
Route parameters are accessed via ctx.params, not ctx.request.params.Step 2: Verify other parts
Async is not required here. Colon before postId is correct for dynamic routes. Template strings are valid for response body.Final Answer:
ctx.request.params is undefined; should use ctx.params -> Option DQuick Check:
Use ctx.params for route parameters [OK]
- Accessing params from ctx.request instead of ctx
- Thinking async is mandatory for all handlers
- Removing colon from dynamic route path
/product/123/review/456 where 123 is the product ID and 456 is the review ID. Which route pattern correctly captures both IDs as parameters?Solution
Step 1: Analyze URL structure
The URL has 'product', then product ID, then 'review', then review ID in order.Step 2: Match route pattern to URL
router.get('/product/:productId/review/:reviewId', (ctx) => { /* ... */ }); matches this pattern exactly with dynamic segments ':productId' and ':reviewId' in correct places.Step 3: Check other options
router.get('/product/:productId/:reviewId/review', (ctx) => { /* ... */ }); misplaces ':reviewId' before 'review' segment. router.get('/product/review/:productId/:reviewId', (ctx) => { /* ... */ }); swaps order. router.get('/product/:productId/review', (ctx) => { /* ... */ }); misses review ID parameter.Final Answer:
router.get('/product/:productId/review/:reviewId', (ctx) => { /* ... */ }); -> Option AQuick Check:
Dynamic segments match URL parts in order [OK]
- Swapping order of dynamic segments and static parts
- Omitting one of the dynamic parameters
- Placing parameters in wrong URL positions
Ready to prove it?
Pass the test and this pattern moves unknown
