Challenge - 5 Problems
Routing Mastery in Deno Oak
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediateWhat is the output of this Deno Oak router code?
Consider this Oak router setup in Deno. What will be the response body when a GET request is made to
/users/42?Deno
import { Application, Router } from "https://deno.land/x/oak/mod.ts"; const router = new Router(); router.get('/users/:id', (context) => { const userId = context.params.id; context.response.body = `User ID is ${userId}`; }); const app = new Application(); app.use(router.routes()); app.use(router.allowedMethods()); // Assume a GET request to /users/42 is made
Attempts:
2 left
💡 Hint
Look at how the route parameter is accessed from context.params.
✗ Incorrect
The route uses a parameter ':id'. When a request is made to '/users/42', the parameter 'id' is '42'. The handler sets the response body to 'User ID is 42'.
📝 Syntax
intermediateWhich option correctly defines a route that matches any path starting with /api?
In Deno Oak, you want to create a route that matches all paths starting with
/api, such as /api/users or /api/products/123. Which route path pattern is correct?Attempts:
2 left
💡 Hint
Check Oak's syntax for matching zero or more segments in a route parameter.
✗ Incorrect
In Oak, ':rest*' matches zero or more path segments after /api. The other patterns are either invalid or do not match as expected.
🔧 Debug
advancedWhy does this Oak router code always return 404 for /posts/123?
Examine this Oak router code. Why does a GET request to
/posts/123 return 404 Not Found?Deno
import { Application, Router } from "https://deno.land/x/oak/mod.ts"; const router = new Router(); router.get('/posts/:postId', (ctx) => { ctx.response.body = `Post ID: ${ctx.params.postId}`; }); const app = new Application(); app.use(router.routes()); app.use(router.allowedMethods()); // Request: GET /posts/123
Attempts:
2 left
💡 Hint
Check the order of app.use calls for middleware.
✗ Incorrect
allowedMethods() must be used after routes() middleware. Otherwise, the router does not handle the route properly and returns 404.
❓ state_output
advancedWhat is the response body after these two routes are added?
Given this Oak router code, what will be the response body for a GET request to
/items/5?Deno
import { Application, Router } from "https://deno.land/x/oak/mod.ts"; const router = new Router(); router.get('/items/new', (ctx) => { ctx.response.body = "Create new item"; }); router.get('/items/:id', (ctx) => { ctx.response.body = `Item ID: ${ctx.params.id}`; }); const app = new Application(); app.use(router.routes()); app.use(router.allowedMethods()); // Request: GET /items/5
Attempts:
2 left
💡 Hint
Check route matching order and parameter parsing.
✗ Incorrect
The route '/items/new' matches '/items/new'. The route '/items/:id' matches '/items/5' with id='5'.
🧠 Conceptual
expertWhich statement about Oak router middleware order is true?
In Deno Oak, you add middleware with
app.use(). You add router.routes() and router.allowedMethods(). Which statement correctly describes the effect of their order?Attempts:
2 left
💡 Hint
Think about how middleware processes requests in sequence.
✗ Incorrect
allowedMethods() relies on routes() to set the matched route. It must come after to send proper 405 or 501 responses.
