{"id":23874112,"url":"https://github.com/vfshera/fastify-dir-routes","last_synced_at":"2026-05-08T16:45:13.953Z","repository":{"id":238897640,"uuid":"797900493","full_name":"vfshera/fastify-dir-routes","owner":"vfshera","description":"A Fastify plugin enabling file system-based routing, utilizing directories to specify URL segments matched by the router.","archived":false,"fork":false,"pushed_at":"2024-05-22T15:35:10.000Z","size":261,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-05T01:48:41.310Z","etag":null,"topics":["fastify","fastify-plugin"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/@vfshera/fastify-dir-routes","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vfshera.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-05-08T17:47:28.000Z","updated_at":"2024-10-26T22:57:45.000Z","dependencies_parsed_at":"2024-05-20T18:49:12.754Z","dependency_job_id":null,"html_url":"https://github.com/vfshera/fastify-dir-routes","commit_stats":null,"previous_names":["vfshera/fastify-dir-routes"],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vfshera%2Ffastify-dir-routes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vfshera%2Ffastify-dir-routes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vfshera%2Ffastify-dir-routes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vfshera%2Ffastify-dir-routes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vfshera","download_url":"https://codeload.github.com/vfshera/fastify-dir-routes/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240233863,"owners_count":19769271,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fastify","fastify-plugin"],"created_at":"2025-01-03T17:52:31.687Z","updated_at":"2026-05-08T16:45:13.850Z","avatar_url":"https://github.com/vfshera.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Fastify Directory Based Routes\n\nA Fastify plugin that enables file system-based routing, utilizing directories to specify URL segments matched by the router.\n\n## Installation\n```sh\npnpm add @vfshera/fastify-dir-routes\n```\n```sh\nnpm i @vfshera/fastify-dir-routes\n```\n\n## Setup\n\nServer\n```ts\nimport fastify, { type FastifyInstance } from \"fastify\";\nimport FastifyDirRoutes from \"@vfshera/fastify-dir-routes\";\n\nconst main = async (): Promise\u003cvoid\u003e =\u003e {\n  const app: FastifyInstance = fastify({ logger: { level: \"debug\" } });\n\n  await app.register(FastifyDirRoutes, {\n    routesDir: \"./routes\",\n    prefix: \"/api\",\n  });\n\n  app.printRoutes();\n  await app.listen({ port: 3000 });\n};\nmain().catch((error) =\u003e { \n  console.log(error);\n});\n\n```\nDirectory structure\n\n```sh\n├── src\n   ├── server.ts\n└──routes\n    ├── (common)\n    │   ├── booking\n    │   │   └── route.ts  -- /api/booking\n    │   └── route.ts\n    ├── _ignore_me        -- will be ignored\n    │   └── route.ts\n    ├── profile\n    │   └── [...all]\n    │       └── route.ts  -- /api/profile/*\n    ├── route.ts          -- /api/\n    └── user\n        └── [id]-[name]\n            ├── route.ts  -- /api/user/:id-:name\n            └── settings  -- will be ignored because there is no route.ts\n```\n\n## Usage\nExport a function with HTTP Method name in uppercase as the function name.\n\nIn `routes/user/[id]-[name]` which resolves to `/api/user/:id-:name`\n```ts\n\nimport type { RouteHandler } from  \"@vfshera/fastify-dir-routes\";\n\n// handles post request to /api/user/:id-:name\nexport const POST: RouteHandler\u003c{\n  Params: { id: string; name: string };\n}\u003e = async (req, res) =\u003e {\n\n  res.send({ message: `Hello ${req.params.name} with id ${req.params.id}` });\n};\n\n\n```\nHandler with Options.\n\n`routes/(common)/booking` resolves to `/api/booking`\n\n```ts \n\nimport type { RouteHandler } from \"@vfshera/fastify-dir-routes\";\n\n// handles post request to /api/booking\nexport const POST: RouteHandler\u003c{\n  Body: { name: string };\n  Reply: { message: string };\n}\u003e = async (request, reply) =\u003e {\n  reply.send({ message: `Thank you for booking ${request.body.name}!` });\n};\n\nPOST.opts = {\n  schema: {\n    body: {\n      type: \"object\",\n      properties: {\n        name: {\n          type: \"string\",\n        },\n      },\n      required: [\"name\"],\n    },\n  },\n};\n\n```\n\nCatch All route (wildcard route)\n\n`routes/profile/[...all]` resolves to `/api/profile/*`\n\nThe plugin provides the wildcard value in params with same name used in directory.\n\n`[...all]` gives adds `all` property in params. Accessed as `req.params.all`\n\n`[...slug]` will provides `slug` in params. Accessed as `req.params.slug`\n\n```ts\n\nimport type { RouteHandler } from \"@vfshera/fastify-dir-routes\";\n\n// handles get request to /api/profile/*\nexport const GET: RouteHandler\u003c{\n  Params: { all: string };\n  Reply: { message: string };\n}\u003e = async (req, res) =\u003e {\n\n  return res.send({ message: `Catched all: '${req.params.all}'` });\n};\n\n```\n\n[**Check the examples folder for more**](./examples/typescript/server.ts)\n\n## Acknowledgements\n\nThis project builds upon Great work from the following packages:\n\n- [Fastify File Routes](https://github.com/spa5k/fastify-file-routes)\n- [Fastify Now](https://github.com/yonathan06/fastify-now)\n\n\n## License\n\n[MIT](./LICENSE)\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvfshera%2Ffastify-dir-routes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvfshera%2Ffastify-dir-routes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvfshera%2Ffastify-dir-routes/lists"}