Skip to content

Latest commit

 

History

History
34 lines (30 loc) · 1.14 KB

File metadata and controls

34 lines (30 loc) · 1.14 KB
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.

import { App } from "fresh";

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.