Challenge - 5 Problems
Middleware Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediateMiddleware Execution Order in Deno
Consider a Deno server using middleware functions added in this order:
1. Middleware A logs "Start A" and calls next()
2. Middleware B logs "Start B" and calls next()
3. Middleware C logs "Start C" and does NOT call next()
What will be the order of console logs when a request is handled?
1. Middleware A logs "Start A" and calls next()
2. Middleware B logs "Start B" and calls next()
3. Middleware C logs "Start C" and does NOT call next()
What will be the order of console logs when a request is handled?
Deno
import { Application } from "https://deno.land/x/oak/mod.ts"; const app = new Application(); app.use(async (ctx, next) => { console.log("Start A"); await next(); console.log("End A"); }); app.use(async (ctx, next) => { console.log("Start B"); await next(); console.log("End B"); }); app.use(async (ctx, next) => { console.log("Start C"); // no next() call here }); app.listen({ port: 8000 });
Attempts:
2 left
💡 Hint
Remember that middleware calls next() to pass control and that code after await next() runs after downstream middleware finish.
✗ Incorrect
Middleware A calls next(), so control passes to B. B calls next(), so control passes to C. C does not call next(), so the chain stops there. Then B resumes after await next(), logging "End B", then A resumes, logging "End A".
❓ state_output
intermediateModifying Context in Middleware
In a Deno Oak app, middleware A sets ctx.state.user = 'Alice'. Middleware B reads ctx.state.user and appends ' logged in'. What is the value of ctx.state.user after both middlewares run?
Deno
import { Application } from "https://deno.land/x/oak/mod.ts"; const app = new Application(); app.use(async (ctx, next) => { ctx.state.user = "Alice"; await next(); }); app.use(async (ctx, next) => { ctx.state.user += " logged in"; await next(); }); app.use((ctx) => { ctx.response.body = ctx.state.user; }); app.listen({ port: 8000 });
Attempts:
2 left
💡 Hint
ctx.state is shared across middleware during a request.
✗ Incorrect
Middleware A sets ctx.state.user to "Alice". Middleware B appends " logged in" to that string. So the final value is "Alice logged in".
📝 Syntax
advancedIdentify the Syntax Error in Middleware
Which middleware function below will cause a syntax error in Deno Oak?
Attempts:
2 left
💡 Hint
Check for missing semicolons or syntax mistakes.
✗ Incorrect
Option A is missing a semicolon after await next(), causing a syntax error in JavaScript/TypeScript.
🔧 Debug
advancedWhy Does This Middleware Not Pass Control?
Given this middleware, why does the request hang and never reach the response handler?
async (ctx, next) => { console.log("Middleware start"); }
async (ctx, next) => { console.log("Middleware start"); }
Deno
async (ctx, next) => { console.log("Middleware start"); }
Attempts:
2 left
💡 Hint
Middleware must call next() to continue the chain.
✗ Incorrect
Middleware must call await next() to pass control to the next middleware. Without it, the request hangs.
🧠 Conceptual
expertMiddleware Pattern Purpose in Deno
What is the main purpose of using middleware patterns in a Deno web server framework like Oak?
Attempts:
2 left
💡 Hint
Think about how middleware helps organize code and handle requests step-by-step.
✗ Incorrect
Middleware allows splitting request handling into small, reusable functions that can modify the request or response and pass control along a chain.
