{"id":19664890,"url":"https://github.com/small-tech/web-routes-from-files","last_synced_at":"2026-03-16T15:01:12.861Z","repository":{"id":42713689,"uuid":"259699234","full_name":"small-tech/web-routes-from-files","owner":"small-tech","description":"Recursively traverses a given directory structure and uses convention to create a list of web route objects that map url paths to JavaScript callback files.","archived":false,"fork":false,"pushed_at":"2023-03-05T11:30:11.000Z","size":160,"stargazers_count":4,"open_issues_count":3,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-30T22:05:12.911Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"agpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/small-tech.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":"2020-04-28T16:58:14.000Z","updated_at":"2023-05-24T14:28:26.000Z","dependencies_parsed_at":"2024-11-11T16:29:48.223Z","dependency_job_id":null,"html_url":"https://github.com/small-tech/web-routes-from-files","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/small-tech%2Fweb-routes-from-files","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/small-tech%2Fweb-routes-from-files/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/small-tech%2Fweb-routes-from-files/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/small-tech%2Fweb-routes-from-files/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/small-tech","download_url":"https://codeload.github.com/small-tech/web-routes-from-files/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252523728,"owners_count":21761967,"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":[],"created_at":"2024-11-11T16:19:31.723Z","updated_at":"2026-03-16T15:01:12.787Z","avatar_url":"https://github.com/small-tech.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Web routes from files\n\nRecursively traverses a given directory structure and uses convention to create a list of web route objects that map Express-style URL paths to JavaScript callback files.\n\n## Installation\n\n```shell\nnpm i @small-tech/web-routes-from-files\n```\n\n## Usage\n\n```js\nconst getRoutes = require ('web-routes-from-files')\n\nconst routes = getRoutes('.')\n\nroutes.forEach(route =\u003e {\n  console.log(`${route.path}: ${route.callback}`)\n})\n```\n\n## Details\n\nGiven the following directory structure:\n\n```sh\n.routes\n   ├─── index.js\n   ├─── my-folder\n   │       ├── index.cjs\n   │       └── other.js\n   ├─── person\n   │       └── index_personId__book_bookId.js\n   ├─── rabbit_rabbitName.js\n   └─── neat.mjs\n```\n\nAnd the following invocation:\n\n```js\nconst getRoutes = require ('web-routes-from-files')\nconst routes = getRoutes('.routes')\n```\n\nYou will get the following data structure:\n\n```js\n[\n  { path: '/', callback: '.routes/index.js' },\n  { path: '/my-folder', callback: '.routes/my-folder/index.js' },\n  { path: '/my-folder/other', callback: '.routes/my-folder/other.js' },\n  { path: '/person/:personId/book/:bookId', callback: '.routes/person/index_personId__book_bookId.js' },\n  { path: '/rabbit/:rabbitName', callback: '.routes/rabbit_rabbitName.js' },\n  { path: '/neat', callback: '.routes/neat.mjs' }\n]\n```\n\nWhich, for example, you could pass to an [Express](https://expressjs.com/) app (as [Site.js](https://sitejs.org)) does:\n\n\n```js\nconst path      = require('path')\nconst express   = require('express')\nconst getRoutes = require ('..')\n\nconst app = express()\nconst routes = getRoutes(path.join(__dirname, '.routes'))\n\n// Note that while .mjs and .cjs files are supported,\n// this module cannot defy the laws of ECMAScript. So the\n// same rules defining mixing of CommonJS and ESM apply here too.\nif (!route.callback.endsWith('.mjs')) {\n  app.get(route.path, require(route.callback))\n}\n\napp.listen(8080, () =\u003e console.log('\\nServer running on http://localhost:8080'))\n```\n\nYour routes should export standard middleware-style functions. e.g.,\n\n```js\nfunction route (request, response, next) {\n  response.end('Hello, world!')\n}\nmodule.exports = route\n```\n\nRoutes that take parameters (introduced in version 3.0.0) can access them from the `request.params` object. e.g., in the `.routes/person/index_personId__book_bookId.js` callback file:\n\n```js\nfunction route (request, response, next) {\n  response.end(`person/${request.params.personId}/book/${request.params.bookId}`)\n}\nmodule.exports = route\n```\n\nThis example is included in the source code. Run it with:\n\n```sh\nnode example\n```\n\n__Note:__ This module will ignore `node_modules` folders and any folder within the root folder being traversed that begins with a dot (i.e., any hidden folder).\n\n## Like this? Fund us!\n\n[Small Technology Foundation](https://small-tech.org) is a tiny, independent not-for-profit.\n\nWe exist in part thanks to patronage by people like you. If you share [our vision](https://small-tech.org/about/#small-technology) and want to support our work, please [become a patron or donate to us](https://small-tech.org/fund-us) today and help us continue to exist.\n\n## Audience\n\nThis is [small technology](https://small-tech.org/about/#small-technology).\n\nIf you’re evaluating this for a “startup” or an enterprise, let us save you some time: this is not the right tool for you. This tool is for individual developers to build personal web sites and apps for themselves and for others in a non-colonial manner that respects the human rights of the people who use them.\n\n## Copyright\n\n\u0026copy; 2020 [Aral Balkan](https://ar.al), [Small Technology Foundation](https://small-tech.org).\n\n## License\n\n[AGPL version 3.0 or later.](https://www.gnu.org/licenses/agpl-3.0.en.html)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmall-tech%2Fweb-routes-from-files","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsmall-tech%2Fweb-routes-from-files","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsmall-tech%2Fweb-routes-from-files/lists"}