Deno server - file based routing
Overview
This template helps to achive file-based-routing
with oak
and deno
only for creating server
.
Index
Tree
.
βββ dev.ts
βββ main.ts
βββ `src`
β βββ `routes`
β β βββ index.ts
β β βββ hlo.ts
β β βββ `v1`
β β β βββ index.ts
β β β βββ my-name
β β β βββ [name].ts
β β βββ `v2`
β β βββ index.ts
β β βββ kaju.ts
β β βββ my-name
β β βββ index.ts
β β βββ [name].ts
β βββ `database`
β βββ dummy.json
βββ `types`
β βββ routes.ts
βββ deno.json
βββ import_map.json
βββ routes.ts
Routing
-
When a file is added to the
src/routes
directory, it's automatically available as a route. -
The files inside the
src/routes
directory can be used to define most common patterns.
-
Index soutes
- The router will automatically route files named
index
to the root of the directory. src/routes/index.ts
β/
src/routes/blog/index.ts
β/blog
- The router will automatically route files named
-
Nested routes
- The router supports nested files. If you create a nested folder structure, files will automatically be routed in the same way still.
src/routes/blog/first-post.ts
β/blog/first-post
src/routes/dashboard/settings/username.ts
β/dashboard/settings/username
-
Dynamic routes segments
- To match a dynamic segment, you can use the bracket syntax. This allows you to match named parameters.
src/routes/blog/[slug].ts
β/blog/:slug
(/blog/hello-world
)src/routes/[username]/settings.ts
β/:username/settings
(/foo/settings
)
Usage
-
Development
mode.deno task dev # or deno run --allow-read --allow-write --allow-run --watch=src/ dev.ts
-
Production
mode.deno task start # or deno run --allow-net main.ts
-
Generate
routes.ts
.deno task generate-routes # or deno run --allow-read --allow-write dev.ts --no-server