Skip to main content
Deno 2 is finally here πŸŽ‰οΈ
Learn more

fsrouter release

A file system based router for Deno.

Usage

Given a project with the following folder structure:

my-app/
β”œβ”€ pages/
β”‚  β”œβ”€ blog/
β”‚  β”‚  β”œβ”€ post.ts
β”‚  β”‚  β”œβ”€ index.ts
β”‚  β”œβ”€ about.ts
β”‚  β”œβ”€ index.ts
β”œβ”€ mod.ts

Each β€œroute file” must export a Handler as its default export:

// my-app/pages/blog/post.ts
export default (req: Request) => {
  return new Response("hello world!");
};

Initialize fsrouter with the following mod.ts:

// my-app/mod.ts
import { fsRouter } from "https://deno.land/x/fsrouter@{VERSION}/mod.ts";
import { serve } from "https://deno.land/std@{VERSION}/http/server.ts";

// Use the file system router with base directory 'pages'
serve(await fsRouter("pages"));

Now running:

deno run --allow-read --allow-net my-app/mod.ts

Results in routes being served as follows:

File Route
pages/index.ts /
pages/about.ts /about
pages/blog/index.ts /blog
pages/blog/post.ts /blog/post

Note: since fsrouter requires access to both the network and the file system, --allow-read and --allow-net are required arguments when executing modules.