Skip to content

Routing

keiro supports the following types of routes by default similar to NextJS routing.

  • Directorysrc
    • main.ts
    • Directoryroutes
      • Directoryposts
        • index.ts
        • [post_id].ts
        • Directoryslug
          • […rest].ts
        • Directorycategories
          • [[category_id]].ts
        • Directorytags
          • [[…tags]].ts

This file tree ilustrate all the route types in keiro:

  • src/routes/posts/index.ts: static routes

    • Matches /posts
  • src/routes/posts/[post_id].ts: dynamic routes

    • Matches /posts/1, /posts/abc, /posts/d57cd924-a8fa-4970-bb4f-9a5ba705cc2b
  • src/routes/posts/slug/[...rest].ts: catch-all routes

    • Matches /posts/slug/a/b/c, /posts/slug/one/two
  • src/routes/posts/categories/[[category_id]].ts: optional dynamic route

    • Matches /posts/categories, /posts/categories/food
  • src/routes/posts/tags/[[...tag]].ts: optional catch-all route

    • Matches /posts/tags, /posts/tags/programming/javascript

This is using the DefaultFileSystemRouteMapper, you can create your own route mapper and pass it to the fileSystemRouter middleware.

Special files

keiro also supports middleware and 404 files.

  • src/routes/middleware.ts Called before each route is called
  • src/routes/404.ts Used to send a 404 response when not route is matched