fsrouter |

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.tsEach β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!");
};.js files are fine as well:
// my-app/pages/blog/post.js
export default (req) => {
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"));Now running:
deno run --allow-read --allow-net my-app/mod.tsResults 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 |
During development, you can use Denoβs built-in --watch=<folder> to restart
the server on changes. Providing a bare --watch has the caveat of not being
able to detect new file additions, since by default Deno will watch only files
it can statically discover. By providing a root directory, Deno will be able to
detect new file additions as well:
deno run --allow-read --allow-net --watch=pages my-app/mod.tsNote: since fsrouter requires access to both the network and the file
system, --allow-read and --allow-net are required arguments when executing
modules.