Routing

Mage has a simple mechanism for routing requests to the appropriate handler.

Basic paths

You register handlers against HTTP methods and paths on the Mage app instance.

app.get("/users", ...);

app.post("/users", ...);

app.put("/users/123", ...);

app.delete("/users/123", ...);

app.put("/users/123", ...);

app.patch("/users/123", ...);

app.all("/users", ...);

Path parameters

You can define path parameters that are then available on contexts request object.

app.get("/users/:id", async (c) => {
  const user = await getUserById(c.req.params.id);

  c.json(user);
});

You can capture as many as you like in a single path.

app.get("/users/:userId/posts/:postId", async (c) => {
  const post = await getPostById(
    c.req.params.userId,
    c.req.params.postId
  );

  c.json(post);
});

Wildcards

You can capture multiple paths with a single entry using a wildcard. The captured wildcard will be available on the contexts request object.

app.get("/public/*", async (c) => {
  const filepath = Deno.cwd("./public", c.req.wildcard);

  await c.file(file);
});