import { fsRouter } from "https://deno.land/x/fsrouter@2.11.1/core/entry.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("pages"));
// Or, provide an options (RouterOptions) object:
// serve(await fsRouter("pages"), { bootMessage: false });
Parameters
optional
unnamed 1: RouterOptions = [UNSUPPORTED]An optional options object