A powerful file-based routing for Express.js, Bun, pure Node.js and more
npm install node-file-routersrc="./static/images/logo.png" />





A powerful file-based routing for Node.js.
It simplifies the creation and organization of API services.
You can effortlessly map your project's file structure to your API endpoints, enabling a clear and maintainable codebase.
* Technology Agnostic:
* Express
* Pure Node HTTP
* Bun
* Sockets
* ... whatever compatible with the interface
* 0 dependencies
* CommonJS and ES modules support
* TypeScript support
* 100% test coverage
* Middlewares support
Imagine you have a project with the following structure:
```
api/
├── profile/
│ ├── middlware.ts - middleware for profile
│ └── orders.[post].ts - methods in any filenames
├── catalog/
│ └── [[...tags]].ts - several segments
├── collection/
│ └── [cid]/ - slugs in folders
│ └── products/
│ └── [pid].ts - slugs in files
├── index.ts - root
├── middleware.ts - middleware for all routes
└── _404.ts - not found response
Node File Router will automatically map it to your API endpoints:
* [POST]: /profile/orders → /api/profile/orders.[post].ts/api/catalog/[[...tags]].ts
* /catalog/men/black/denim → /api/collection/[cid]/products/[pid].ts
* /collection/77/products/13 → index.ts
* / →
Some examples of how your file handlers can be written:
Methods in a file
`js`
export default {
get(req, res, routeParams) {},
post(req, res, routeParams) {},
patch(req, res, routeParams) {},
}
Single response function
`js`
export default function (req, res, routeParams) {}
Middlewares chain
`js`
export default [
useErrorHandler,
useLogger,
useAuthGuard,
];
Classes
`js`
export default class Resource {
get(req, res, routeParams) {}
post(req, res, routeParams) {}
patch(req, res, routeParams) {}
}
`bash``
npm i node-file-router
Visit website to get started and view
the full documentation.
Usage:
* Getting started
* Usage guide
* Configuration
* Usage with Bun
* Get All Registered Routes
Methods routing:
* Any method
* Object with methods
* Class with methods
* Methods in filenames
Route matching:
* Plain match - /plain/route/
* Exact slug - /[id]/
* Catching all - /[id1]/[id2]/[idn]/
* Optional catching all - ?/[id1]/[id2]/[idn]/
Support other protocols and frameworks:
* Custom adapter
Middlewares:
* Usage and examples
Visit examples folder to see samples with
different module systems, adapters, and use cases.
I usually respond within 24 hours. Fixes typically take 1-3 days. Additionally, I provide time estimations for
new features and fixes. Feel free to ask questions, voice ideas, and share your projects on Github Discussion.
To begin development and contribution, read this guide.