A routing pattern is a rule that matches a URL path to a specific code or function that handles the request. It helps the server decide what to do when a user visits a certain web address.
Click to reveal answer
beginner
How does a dynamic route differ from a static route?
A static route matches a fixed URL exactly, like '/about'. A dynamic route uses placeholders to match variable parts, like '/user/:id', where ':id' can be any user ID.
Click to reveal answer
intermediate
What does the pattern '/posts/:postId/comments/:commentId' represent?
It represents a nested dynamic route where ':postId' and ':commentId' are variables. This pattern matches URLs like '/posts/123/comments/456' to access a specific comment on a specific post.
Click to reveal answer
beginner
Why are routing patterns important in Deno web applications?
Routing patterns organize how the app responds to different URLs. They make the code cleaner and help users reach the right content or action easily.
Click to reveal answer
intermediate
Explain the use of wildcard routes in routing patterns.
Wildcard routes use symbols like '*' to match any path or part of a path. They are useful for catch-all routes, like showing a 404 page when no other route matches.
Click to reveal answer
Which routing pattern matches the URL '/user/42' if the pattern is '/user/:id'?
AStatic route exactly '/user/:id'
BNo match
CWildcard route matching any URL
DDynamic route with ':id' as 42
✗ Incorrect
The ':id' is a placeholder that matches '42' in the URL, making it a dynamic route match.
What does a wildcard '*' in a route pattern do?
AMatches only the root URL
BMatches any path or subpath
CMatches only static paths
DDisables routing
✗ Incorrect
A wildcard '*' matches any path or subpath, useful for catch-all routes.
In Deno, which module is commonly used for routing?
Aoak
Bexpress
Cstd/http/router.ts
Dkoa
✗ Incorrect
Oak is a popular middleware framework for Deno that includes routing features.
What is the benefit of using dynamic routes?
AThey allow handling many similar URLs with one pattern
BThey reduce server load
CThey improve page styling
DThey disable caching
✗ Incorrect
Dynamic routes let you handle many URLs that differ by some parts, like user IDs, with one pattern.
Which route pattern would match '/products/electronics/123'?
A/products
B/products/*
C/products/:category/:id
D/products/:id
✗ Incorrect
The pattern '/products/:category/:id' matches URLs with two dynamic parts after '/products'.
Describe how routing patterns help a Deno web server handle different URLs.
Think about how the server knows what to do when a user visits a page.
You got /3 concepts.
Explain the difference between a wildcard route and a dynamic route in routing patterns.
Consider how flexible each pattern is in matching URLs.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of routing patterns in a Deno web application?
easy
A. To store data in a database
B. To style the webpage with colors and fonts
C. To connect URLs to specific code that runs when those URLs are visited
D. To manage user authentication only
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 C
Quick Check:
Routing = URL to code connection [OK]
Hint: Routing links URLs to code that runs on visit [OK]
Common Mistakes:
Confusing routing with styling or database tasks
Thinking routing only manages user login
Believing routing changes page colors
2. Which of the following is the correct way to define a static route using Oak in Deno?
easy
A. router.get('/home', (ctx) => { ctx.response.body = 'Welcome'; });
B. router.route('/home', (ctx) => { ctx.body = 'Welcome'; });
C. router.add('/home', (ctx) => { ctx.response.body = 'Welcome'; });
D. router.get('/home', (ctx) => { return 'Welcome'; });
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.
D. ctx.request.params is undefined; should use ctx.params
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 D
Quick Check:
Use ctx.params for route parameters [OK]
Hint: Use ctx.params, not ctx.request.params for route params [OK]
Common Mistakes:
Accessing params from ctx.request instead of ctx
Thinking async is mandatory for all handlers
Removing colon from dynamic route path
5. You want to create a Deno Oak route that matches URLs like /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?
hard
A. router.get('/product/:productId/review/:reviewId', (ctx) => { /* ... */ });
B. router.get('/product/:productId/:reviewId/review', (ctx) => { /* ... */ });
C. router.get('/product/review/:productId/:reviewId', (ctx) => { /* ... */ });
D. router.get('/product/:productId/review', (ctx) => { /* ... */ });
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.