Skip to content

πŸ’« A file-based routing for Node.js. Works with Bun, pure Node.js, Express.js, and more!

License

Notifications You must be signed in to change notification settings

Danilqa/node-file-router

Folders and files

NameName
Last commit message
Last commit date

Latest commit

c716e67 Β· Jan 27, 2025
Jan 27, 2025
Oct 10, 2023
Aug 11, 2023
Jan 25, 2025
Jan 25, 2025
Dec 12, 2023
Jan 25, 2025
Jan 27, 2025
Jan 31, 2024
Mar 10, 2024
Jul 30, 2023
Mar 10, 2024
Aug 11, 2023
Jan 25, 2025
Mar 10, 2024
Jul 30, 2023
Oct 10, 2023
Jan 25, 2025
Oct 10, 2023
Jan 16, 2024

Repository files navigation

Node File Router

product of the week

Codacy Badge ci Status npm downloads license

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

Overview

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
  • /catalog/men/black/denim β†’ /api/catalog/[[...tags]].ts
  • /collection/77/products/13 β†’ /api/collection/[cid]/products/[pid].ts
  • / β†’ index.ts

Some examples of how your file handlers can be written:

Methods in a file

export default {
  get(req, res, routeParams) {},
  post(req, res, routeParams) {},
  patch(req, res, routeParams) {},
}

Single response function

export default function (req, res, routeParams) {}

Middlewares chain

export default [
  useErrorHandler,
  useLogger,
  useAuthGuard,
];

Classes

export default class Resource {
  get(req, res, routeParams) {}
  
  post(req, res, routeParams) {}
  
  patch(req, res, routeParams) {}
}

Documentation

npm i node-file-router

Visit website to get started and view the full documentation.

Usage:

Methods routing:

Route matching:

Support other protocols and frameworks:

Middlewares:

Examples

Visit examples folder to see samples with different module systems, adapters, and use cases.

Support

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.

Developing

To begin development and contribution, read this guide.