Deno JS: Oak Routing

Oak is a middleware framework for Deno that provides a simple and modular way to handle HTTP requests and responses. Here is how to setup basic routing.

In Oak, URL routing is handled by creating a router and defining routes using the Router class. Here's an example:

 1import { Application, Router } from "";
 3const router = new Router();
 5  .get("/", (context) => {
 6    context.response.body = "Hello, world!";
 7  })
 8  .get("/users", (context) => {
 9    context.response.body = "List of users";
10  })
11  .post("/users", async (context) => {
12    const body = await context.request.body();
13    console.log(body.value);
14    context.response.body = "User created";
15  });
17const app = new Application();
21await app.listen({ port: 8000 });

Save the file into server.ts and start using the command below:

1deno --allow-net server.ts

In this example, we create a new Router instance and define three routes: GET /, GET /users, and POST /users. Each route handler takes a context object as an argument, which contains information about the incoming request and response. In the first route handler, we simply set the response body to a greeting. In the second route handler, we set the response body to a list of users. In the third route handler, we retrieve the request body and log it, then set the response body to indicate that a user was created.

Finally, we create a new Application instance and use the router's routes method and allowedMethods method as middleware for handling requests. The routes method maps incoming requests to the appropriate route handler based on the HTTP method and URL path, while the allowedMethods method sends an appropriate response for requests with unsupported HTTP methods.

When we run this application and make requests to the appropriate URLs, the appropriate route handlers will be invoked and the response will be sent back to the client.