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
optional
options: Partial<RouterOptions> = [UNSUPPORTED]An optional options object