Paths and Methods
Learning objectives
- You know how to map paths and methods to functions with Hono.
A route is a mapping from a method-path -combination to a function.
Multiple routes
When working with Hono, each route is explicitly added using the methods provided by Hono. In the previous section, we saw how to add a route for the path /
using the get
method. The code was as follows.
import { Hono } from "https://deno.land/x/hono@v3.12.11/mod.ts";
const app = new Hono();
app.get("/", (c) => c.text("Hello World!"));
Deno.serve(app.fetch);
Adding multiple routes is as simple as adding multiple calls to the get
method, where each call would define a specific path and the function corresponding to the path. The following example outlines an application with two routes. The first route is for the path /
and the second route is for the path /secret
.
import { Hono } from "https://deno.land/x/hono@v3.12.11/mod.ts";
const app = new Hono();
app.get("/", (c) => c.text("Hello World!"));
app.get("/secret", (c) => c.text("Hello Illuminati!"));
Deno.serve(app.fetch);
Additional routes can be added with ease. Let's look at this through another example. When working with the first web applications, one of the assignments asked for an application with the following functionality:
Requests made to the path '/one' receive a response that contains the string 'yksi'. Requests made to the path '/two' receive a response that contains the string 'kaksi'. Requests made to other paths receive a response that contains the string 'pong'.
With Hono, such an application would be implemented as follows.
import { Hono } from "https://deno.land/x/hono@v3.12.11/mod.ts";
const app = new Hono();
app.get("/one", (c) => c.text("yksi"));
app.get("/two", (c) => c.text("kaksi"));
app.get("/*", (c) => c.text("pong"));
Deno.serve(app.fetch);
Asterisks in paths
Asterisks *
in Hono paths are used as wildcards. In the example above, the path /*
matches all paths that have not been matched by any other route.
Request methods
Hono has methods for adding routes where the method names correspond to the HTTP request methods. As an example, the get
method is used to define routes for requests that use the HTTP GET method. In a similar vein, the post
method would be used to define routes for requests that use the HTTP POST method.
The following application outlines the use of the get
and post
methods.
import { Hono } from "https://deno.land/x/hono@v3.12.11/mod.ts";
const app = new Hono();
app.post("/", (c) => c.text("POST request to /"));
app.get("/", (c) => c.text("GET request to /"));
Deno.serve(app.fetch);
When we run the above application and make requests to it, we see that the application responds differently to requests made with HTTP GET and HTTP POST methods.
curl localhost:8000
GET request to /%
curl -X POST localhost:8000
POST request to /%
It is also possible to define non-conventional (and non-existing) methods. This is done using the on
method, which takes the name of the method as the first parameter, the path as the second parameter, and the function to be executed as the third parameter. The following example outlines the use of the on
method for creating an application that responds to HTTP requests where the method is PEEK.
import { Hono } from "https://deno.land/x/hono@v3.12.11/mod.ts";
const app = new Hono();
app.on("peek", "/", (c) => c.text("Nothing to see here."));
Deno.serve(app.fetch);
When we run the above application, we see that the application responds to requests made with the PEEK method. On the other hand, the server responds with not found to other requests.
curl -X PEEK localhost:8000
Nothing to see here.%
curl localhost:8000
404 Not Found%
Determining path and method
The path and method of a request are also included in a req
property of the context. The following example outlines an application that would listen to requests made with GET method to any path, and then respond with the method and the path.
import { Hono } from "https://deno.land/x/hono@v3.12.11/mod.ts";
const app = new Hono();
app.get("*", (c) => c.text(`${c.req.method} ${c.req.path}`));
Deno.serve(app.fetch);
Trying the application out, we see that the paths (and the method) are included in the response.
curl localhost:8000
GET /%
curl localhost:8000/hello
GET /hello%
curl -X POST localhost:8000
404 Not Found%