Skip to main content
The Deno 2 Release Candidate is here
Learn more
Module

x/fsrouter/core/handler.ts>fsRouter

A file system based router for Deno.
Latest
function fsRouter
import { fsRouter } from "https://deno.land/x/fsrouter@3.1.0/core/handler.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