Complete the code to define a route handler that responds with 'Hello World'.
import { serve } from "https://deno.land/std/http/server.ts"; serve((req) => { return new Response("[1]", { status: 200 }); });
The Response constructor expects a string or body as the first argument. We pass the string "Hello World" to respond with that text.
Complete the code to import the Router from Oak framework.
import { [1] } from "https://deno.land/x/oak/mod.ts";
The Router is imported from Oak to define routes and layouts.
Fix the error in the route definition to respond with JSON data.
router.get('/data', (ctx) => { ctx.response.[1] = { message: 'Success' }; });
In Oak, to send JSON data, assign the object to ctx.response.body. Oak automatically sets the content type.
Fill both blanks to create a layout middleware that adds a header and footer around the content.
async function layout(ctx, next) {
ctx.response.body = '<header>Header</header>' + [1];
await next();
ctx.response.body += [2];
}We wrap the existing body content by first adding the header, then the existing body (ctx.response.body), then after next middleware, append the footer.
Fill all three blanks to define a route with a dynamic parameter and respond with it.
router.get('/user/[1]', (ctx) => { const id = ctx.params.[2]; ctx.response.body = { userId: [3] }; });
The route uses :id to capture the dynamic segment. We access it via ctx.params.id and respond with that value.
