{"_id":"@dword-design/express-file-routing","name":"@dword-design/express-file-routing","dist-tags":{"latest":"0.0.1"},"versions":{"0.0.1":{"name":"@dword-design/express-file-routing","version":"0.0.1","description":"Simple system-based file routing for Express","author":{"name":"Matthias Halfmann"},"repository":{"type":"git","url":"git+https://github.com/matthiaaas/express-file-routing.git"},"main":"dist/index.js","types":"dist/index.d.ts","scripts":{"test":"echo \"Error: no test specified\" && exit 1","build":"rollup -c","dev":"ts-node-dev --transpile-only --files --quiet ./examples/with-typescript/app.ts"},"keywords":["express","api","file","router","routing","typescript"],"license":"MIT","peerDependencies":{"express":">4.1.2"},"devDependencies":{"@types/express":"^4.17.13","babel-core":"^6.26.3","babel-runtime":"^6.26.0","express":"^4.17.3","rollup":"^2.68.0","rollup-plugin-typescript2":"^0.31.2","ts-node-dev":"^1.1.8","typescript":"^4.6.2"},"publishConfig":{"access":"public"},"gitHead":"367bb59c19dcc0ed8bb3fe009469092559a64ffc","bugs":{"url":"https://github.com/matthiaaas/express-file-routing/issues"},"homepage":"https://github.com/matthiaaas/express-file-routing#readme","_id":"@dword-design/express-file-routing@0.0.1","_nodeVersion":"16.18.1","_npmVersion":"8.19.2","dist":{"integrity":"sha512-mBu9mkyeDiMvML9ws+UsJ5YLC4KRKwkjARBYcg0CVMnwNL2j1Hlzs+2JbXlgKcosLP5jR44JbhUCajsx709JLg==","shasum":"eb23a18bd84e4272641a2763470670c6b384a408","tarball":"https://registry.npmjs.org/@dword-design/express-file-routing/-/express-file-routing-0.0.1.tgz","fileCount":10,"unpackedSize":28611,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIGQP1snKHxFp5a5/n6kZFiPY9uCrgc1fWilEAXatWE+lAiAoWH9esmQiaC79OGxUDTIyv63pNsTVjphCQKUvQw0Ojg=="}],"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v4.10.10\r\nComment: https://openpgpjs.org\r\n\r\nwsFzBAEBCAAGBQJjf501ACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b\r\nEgJ2Vmr/eBAAisFtsF8iblkRAp8RJFuluVRsgM0YQZw1oAWC74yyoA5jWz3o\r\nmlgl/VfVnptIN/X/4vEiI518r+vl2ceOeRXumU5hRp+qJyqPCpqf/KwDsq56\r\nwsMva4PO+N1BRAyiapIlmsCoWNSycwWVqCQ6MtYuVkgq8wPVu5wR0V2OSlb4\r\n2flruZqZLS75oE6amYNAb7aDZ5Hy3f1LwGNjwesxYoCAil0Yw0qf/tbx9YvR\r\n34bd/88Z0j3e/vO+Cwow+ftCF1xfTs0qRZqNjuj0CYy/BcPZtCL/EbwElfY7\r\nF2JEZAZV7VJbBb5dPI1Qqz/XV8ghkxah+I6wT8HxCkr+DM5JXd6KFqwYwA6y\r\n5jRxRCqvY1gfILRC2hasVe9XufzDtd1XgikZWvmMLH12k+WslbiGTC4H899O\r\n/G83MnzYZyts4NK38h5pyqLkaioFlSAM5frtJZmmFjfI3CMqD3k9r1zgxtNu\r\nRHEUY1JyCJoMoWnISjh6TnoTflAn8C4aGOBiy7IqCgpq4xGJA6UOY3N3tx9P\r\nB36cD1ugRe02m5WMMRl8m92xGBuJsSyBQrTMKi8wquaWzSSNtZzaqKMjz0Rh\r\nA1nC5wRhdvg/hy1zkPtOfX+78Z0bJn7a5TzfKTE1kA08U+N+OuuAw4blSL9+\r\nJErZe9KteYTM8xj2/hpN6gzibKPToPtnAFs=\r\n=1863\r\n-----END PGP SIGNATURE-----\r\n"},"_npmUser":{"name":"dword-design","email":"info@sebastianlandwehr.com"},"directories":{},"maintainers":[{"name":"dword-design","email":"info@sebastianlandwehr.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/express-file-routing_0.0.1_1669307701588_0.8238671521267096"},"_hasShrinkwrap":false}},"time":{"created":"2022-11-24T16:35:01.532Z","0.0.1":"2022-11-24T16:35:01.767Z","modified":"2022-11-24T16:35:01.913Z"},"maintainers":[{"name":"dword-design","email":"info@sebastianlandwehr.com"}],"description":"Simple system-based file routing for Express","homepage":"https://github.com/matthiaaas/express-file-routing#readme","keywords":["express","api","file","router","routing","typescript"],"repository":{"type":"git","url":"git+https://github.com/matthiaaas/express-file-routing.git"},"author":{"name":"Matthias Halfmann"},"bugs":{"url":"https://github.com/matthiaaas/express-file-routing/issues"},"license":"MIT","readme":"# express-file-routing\n\n![GitHub release (latest by date)](https://img.shields.io/github/v/release/matthiaaas/express-file-routing?color=brightgreen&label=latest)\n\nFlexible system-based file routing for Express with `0` dependencies.\n\n## Installation\n\n```bash\nnpm install express-file-routing\n```\n\n**Note:** If you prefer `yarn` instead of `npm`, just use `yarn add express-file-routing`.\n\n## How to use\n\n- app.ts (main)\n\n```ts\nimport express from \"express\"\nimport createRouter, { router } from \"express-file-routing\"\n\nconst app = express()\n\n// Option 1\napp.use(\"/\", router()) // as router middleware or\n\n// Option 2\ncreateRouter(app) // as wrapper function\n\napp.listen(2000)\n```\n\n**Note:** It uses your project's `/routes` directory as source by default.\n\n- routes/index.ts\n\n```ts\nexport default async (req, res) => {\n  if (req.method !== \"GET\") return res.status(405)\n\n  return res.json({ hello: \"world\" })\n}\n```\n\n#### Directory Structure\n\nFiles inside your project's `/routes` directory will get matched an url path automatically.\n\n```php\n├── app.ts\n├── routes\n    ├── index.ts // index routes\n    ├── posts\n        ├── index.ts\n        └── :id.ts or [id].ts // dynamic params\n    └── users.ts\n└── package.json\n```\n\n- `/routes/index.ts` → /\n- `/routes/posts/index.ts` → /posts\n- `/routes/posts/[id].ts` → /posts/:id\n- `/routes/users.ts` → /users\n\n**Note:** Files prefixed with an underscore or ending with `.d.ts` are excluded from route generation.\n\n## API\n\n```ts\ncreateRouter(app, {\n  directory: path.join(__dirname, \"routes\"),\n  additionalMethods: [\"ws\", ...]\n})\n// or\napp.use(\"/\", router({\n  directory: path.join(__dirname, \"routes\"),\n  additionalMethods: [\"ws\", ...]\n}))\n```\n\n### Options\n\n- `directory`: The path to the routes directory (defaults to `/routes`)\n- `additionalMethods`: Additional methods that match an export from a route like `ws` (e.g. `ws` for express-ws)\n\n## Examples\n\n### HTTP Method Matching\n\nIf you export functions named e.g. `get`, `post`, `put`, `patch`, `delete`/`del` [etc.](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods) those will get matched their corresponding http method automatically.\n\n```ts\nexport const get = async (req, res) => { ... }\n\nexport const post = async (req, res) => { ... }\n\n// it's not allowed to name variables 'delete': try 'del' instead\nexport const del = async (req, res) => { ... }\n\n// you can still use a wildcard default export in addition\nexport default async (req, res) => { ... }\n```\n\n**Note:** Named method exports gain priority over wildcard exports (= default exports).\n\n### Middlewares\n\nYou can add isolated, route specific middlewares by exporting an array of Express request handlers from your route file.\n\n```ts\nimport { rateLimit, bearerToken, userAuth } from \"../middlewares\"\n\nexport const get = [\n  rateLimit(), bearerToken(), userAuth(),\n  async (req, res) => { ... }\n]\n```\n\nA middleware function might look like the following:\n\n```ts\n// /middlewares/userAuth.ts\nexport default (options) => async (req, res, next) => {\n  if (req.authenticated) next()\n  ...\n}\n```\n\n### Custom Methods Exports\n\nYou can add support for other method exports to your route files. This means that if your root app instance accepts non built-in handler invocations like `app.ws(route, handler)`, you can make them being recognized as valid handlers.\n\n```ts\n// app.ts\nimport ws from \"express-ws\"\n\nconst { app } = ws(express())\n\ncreateRouter(app, {\n  additionalMethods: [\"ws\"]\n})\n\n// routes/index.ts\nexport const ws = async (ws, req) => {\n  ws.send(\"hello world\")\n}\n```\n\n### Usage with TypeScript\n\nAdding support for route & method handler type definitions is as straightforward as including Express' native `Handler` type from [@types/express](https://www.npmjs.com/package/@types/express).\n\n```ts\n// /routes/posts.ts\nimport type { Handler } from \"express\"\n\nexport const get: Handler = async (req, res, next) => { ... }\n```\n","readmeFilename":"README.md"}