Files
sweet/docs/canary/concepts/routing.md
T
Marvin Hagemeister a703e79181 docs: update canary docs for Fresh 2 (#3108)
There are still some rough edges here and there, but it's in a much
better shape than what we have today.
2025-07-18 15:18:38 +02:00

1.1 KiB

description
description
File based routing is the simplest way to do routing in Fresh apps. Additionally custom patterns can be configured per route.

Routing defines which middlewares and routes should respond to a particular request.

const app = new App()
  .get("/", () => new Response("hello")) // Responds to: GET /
  .get("/other", () => new Response("other")) // Responds to: GET /other
  .post("/upload", () => new Response("upload")); // Responds to: POST /upload
  .get("/books/:id", (ctx) => {
    // Responds to: GET /books/my-book, /books/cool-book, etc
    const id = ctx.params.id
    return new Response(`Book id: ${id}`));
  })
  .get("/blog/:post/comments", () => {
    // Responds to: GET /blog/my-post/comments, /blog/hello/comments, etc
    const post = ctx.params.post
    return new Response(`Blog post comments for post: ${post}`)
  })
  .get("/foo/*", (ctx) => {
    // Responds to: GET /foo/bar, /foo/bar/baz, etc
    return new Response("foo"));
  })

Fresh supports the full URLPattern syntax for setting pathnames.