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

x/fsrouter/mod.ts>fsRouter

A file system based router for Deno.
Latest
function fsRouter
import { fsRouter } from "https://deno.land/x/fsrouter@3.1.0/mod.ts";

fsRouter creates a standard library Handler which handles requests according to the shape of the filesystem at the given rootDir. Each file within rootDir must provide a FsHandler as its default export, which will be used to execute requests if the requested route matches the file's position in the filesystem. See docs on FsHandler.

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 FsHandler as its default export:

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

Initialize a server by calling fsRouter:

// 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(import.meta.resolve("./pages")));

// Or, provide an options (RouterOptions) object:
// serve(await fsRouter(import.meta.resolve("./pages")), { bootMessage: false });

Parameters

rootDir: string

The directory at which routes will be served

optional
options: Partial<RouterOptions> = [UNSUPPORTED]

An optional options object

Returns

Promise<http.Handler>

A Promise which resolves to a Handler