{"id":20046172,"url":"https://github.com/spa5k/fastify-file-routes","last_synced_at":"2025-06-24T04:02:40.615Z","repository":{"id":39496671,"uuid":"439670859","full_name":"spa5k/fastify-file-routes","owner":"spa5k","description":"A Fastify plugin that provides a file system routes, based on the way Next.JS file system routing works, including all possible features.","archived":false,"fork":false,"pushed_at":"2025-04-10T22:55:37.000Z","size":69605,"stargazers_count":36,"open_issues_count":13,"forks_count":2,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-06-07T03:03:51.207Z","etag":null,"topics":["fastify","fastify-plugin","filesystem","hacktoberfest","nextjs","routing"],"latest_commit_sha":null,"homepage":"","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/spa5k.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,"zenodo":null}},"created_at":"2021-12-18T17:06:59.000Z","updated_at":"2025-04-03T17:30:18.000Z","dependencies_parsed_at":"2023-12-16T04:47:16.678Z","dependency_job_id":"ae1a7ab5-7ac8-4ff8-9b2d-112b834172ca","html_url":"https://github.com/spa5k/fastify-file-routes","commit_stats":{"total_commits":26,"total_committers":2,"mean_commits":13.0,"dds":0.2692307692307693,"last_synced_commit":"cf0d8cc7fa694dc1cbd0c4b8c57e8664f66f2b8a"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":"spa5k/esbuild-typescript-library-template","purl":"pkg:github/spa5k/fastify-file-routes","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spa5k%2Ffastify-file-routes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spa5k%2Ffastify-file-routes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spa5k%2Ffastify-file-routes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spa5k%2Ffastify-file-routes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spa5k","download_url":"https://codeload.github.com/spa5k/fastify-file-routes/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spa5k%2Ffastify-file-routes/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261601357,"owners_count":23183082,"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","filesystem","hacktoberfest","nextjs","routing"],"created_at":"2024-11-13T11:21:36.211Z","updated_at":"2025-06-24T04:02:40.532Z","avatar_url":"https://github.com/spa5k.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Fastify File Routes\n\n\u003cdiv align='center'\u003e\n\n![Banner](./banner.png)\n\n[![NPM downloads](https://img.shields.io/npm/dm/fastify-file-routes.svg?style=for-the-badge)](https://www.npmjs.com/package/fastify-file-routes)\n[![npm](https://img.shields.io/npm/v/fastify-file-routes?logo=npm\u0026style=for-the-badge)](https://www.npmjs.com/package/fastify-file-routes)\n![node-current](https://img.shields.io/badge/Node-%3E=14-success?style=for-the-badge\u0026logo=node)\n\nA Fastify plugin that provides a file system routes, based on the way Next.JS file system routing works, including all possible features.\n\n\u003c/div\u003e\n\n## :sparkles: Features\n\n    1. File System Routing.\n    2. Index Routes.\n    3. Nested Routes.\n    4. Dynamic Route Segments.\n    5. Catch All (Wildcard \\*) Routes.\n    6. Multiple parameters. eg /users/:id-:name\n\n## :rocket: Installation\n\n```sh\nnpm install fastify-file-routes\n```\n\n```yarn\nyarn add fastify-file-routes\n```\n\n## :blue_book: Usage/Examples\n\n### 1. Register the Plugin.\n\n```typescript\nimport { fileRoutes } from \"fastify-file-routes\";\n\nconst app: FastifyInstance = fastify({ logger: true });\n\nawait app.register(fileRoutes, {\n  routesDir: \"./routes\",\n  prefix, // -\u003e optional\n});\n\nawait app.listen(3000);\n```\n\n### 2. Create the routes directory.\n\n```sh\nmkdir routes\n```\n\n### 3. Create your first route in the routes directory\n\n```typescript\n//file: `routes/some/route.ts`\n//url:  `http://localhost/some/route`\n\nimport type { Route } from \"fastify-file-routes\";\n\nexport const routes: Route = {\n  get: {\n    handler: async (request, reply) =\u003e {\n      await reply.send({\n        some: \"route\",\n      });\n    },\n  },\n};\n```\n\n### 4. Access the Parameters.\n\n```typescript\n//file: `routes/users/[userId]/settings.js`\n//mapped to: `http://localhost/users/:userId/settings`\n\nexport const routes: Route = {\n  get: {\n    handler: async (request, reply) =\u003e {\n      const { params } = request;\n      await reply.send(`photos of user ${params.userId}`);\n    },\n  },\n};\n```\n\n### 5. Wildcard (\\*) routes.\n\n```typescript\n//file: `routes/profile/[...id].ts  `\n//mapped to: `http://localhost/profile/*`\n\nexport const routes: Route = {\n  get: {\n    handler: async (request, reply) =\u003e {\n      const { params } = request;\n      await reply.send(`wildcard route`);\n    },\n  },\n};\n```\n\n### 6. Post Request..\n\n```typescript\nexport const routes: Route = {\n  post: {\n    handler: async (_request, reply) =\u003e {\n      await reply.send({\n        post: \"post user\",\n      });\n    },\n  },\n};\n```\n\n### 7. Prefix Route\n\n```typescript\n//file: `routes/some/route.ts`\n//url:  `http://localhost/api/some/route`\n\nawait app.register(fileRoutes, {\n  routesDir: \"./routes\",\n  prefix: \"/api\",\n});\n\nexport const routes: Route = {\n  post: {\n    handler: async (_request, reply) =\u003e {\n      await reply.send({\n        post: \"post user\",\n      });\n    },\n  },\n};\n```\n\n### 8. Multiple Parameters\n\n```typescript\n//file: `routes/some/[param1]-[param2].ts`\n//url:  `http://localhost/some/:param1-:param2`\n\nawait app.register(fileRoutes, {\n  routesDir: \"./routes\",\n});\n\nexport const routes: Route = {\n  post: {\n    handler: async (_request, reply) =\u003e {\n      await reply.send({\n        post: \"multiple params\",\n      });\n    },\n  },\n};\n```\n\n## :information_source: Info\n\n1. Check the examples folder in /examples to see how to use the plugin.\n2. route.prefixTrailingSlash has been set to 'both'.\n\n## :arrow_forward: Route module definition\n\nMethod specification for attributes is available here: [Method specification](https://www.fastify.io/docs/latest/Routes/#full-declaration)\n\n\u003e :information_source: attributes `url` and `method` are dynamically provided\n\nAllowed attributes mapped to Http methods in module:\n\n- delete\n- get\n- head\n- patch\n- post\n- put\n- options\n\n## :arrow_forward: Skipping files\n\nto skip file in routes directory, prepend the `.` or `_` character to filename\n\nexamples:\n\n```text\nroutes\n├── .ignored-directory\n├── _ignored-directory\n├── .ignored-js-file.js\n├── _ignored-js-file.js\n├── .ignored-ts-file.ts\n├── _ignored-ts-file.ts\n├── ignored-js-test.test.js\n└── ignored-ts-test.test.ts\n```\n\n\u003e :warning: also any `*.test.js` and `*.test.ts` are skipped!\n\nthis is useful if you want to have a lib file which contains functions that don't have to be a route, so just create the file with `_` prepending character\n\n## TODO\n\n1. Adding support for optional wildcard routes - [[...id]].\n2. More tests.\n3. Better typescript stuff for validation and inferences in routes.\n\n## Visualization of this Repo.\n\n![Visualization of this repo](./diagram.svg)\n\n## License\n\n[MIT](https://choosealicense.com/licenses/mit/)\n\n## Related/Acknowledgements\n\n[Fastify - AutoRoutes](https://github.com/GiovanniCardamone/fastify-autoroutes) - Lots of code has been used from this project.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspa5k%2Ffastify-file-routes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspa5k%2Ffastify-file-routes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspa5k%2Ffastify-file-routes/lists"}