0.0.8 • Published 6 months ago

express-tree-routing v0.0.8

Weekly downloads
-
License
ISC
Repository
github
Last release
6 months ago

Express Tree Routing Module

npm version

Express Tree Routing is a powerful module that enables dynamic routing and middleware configuration based on the directory structure of your Express.js project. It simplifies the process of setting up routes and middleware by automatically scanning directories and files and attaching them to your Express router.

Features

  • Automatic tree-based routing: Routes are defined based on the directory structure of your project, eliminating the need to manually configure routes for each directory.
  • Easy middleware configuration: Middleware can be organized and attached at various levels within the directory structure, providing flexibility and modularity.
  • Supports dot-based middleware inclusion: You can specify additional middleware to be applied based on specific filenames, allowing for fine-grained control over middleware execution.

Installation

Install the package using npm:

npm install express-tree-routing

Usage

const express = require('express');
const treeRouting = require('express-tree-routing');
const path = require("path");

const app = express();
const router = express.Router();

// Specify the base directory of your routers
const baseDirectory = path.join(__dirname, "routers");

// Configure tree routing module
const registeredRoutes = treeRouting(baseDirectory, router);

// Attach the router to your Express app
app.use(registeredRoutes);
// or
app.use("/api", registeredRoutes);

// Start the server
app.listen(3000, () => {
  console.log('Server started on port 3000');
});

Example

Let's consider the following directory structure:

├── routes
│   ├── users
│   │   ├── middleware.js
│   │   └── index.js
│   ├── posts
|   |   ├── :POST_ID
|   |   |   ├── comments
|   |   |   |   ├── :COMMENT_ID
|   |   |   |   |   ├── replies
|   |   |   |   |   |   ├── :REPLY_ID.js
|   |   |   |   |   |   └── index.js
|   |   |   |   |   └── index.js
|   |   |   └── author.js
│   │   ├── middleware.js
│   │   └── index.js
│   ├── middleware.js
│   ├── tuturials.js
│   └── index.js
  • Each directory represents a route, and the corresponding index.js file inside the directory defines the index route.
  • The middleware.js files contain middleware specific to that directory or route.
  • The routes/middleware.js file contains middleware that applies to all routes in that directory.

Each file in directories except middleware.js (example: index.js, comments.js) must return the router which passed to it by module as below:

module.exports = (router) => {
  router.get(/* arguments */);
  return router;
}

The treeRouting function will automatically scan base directory structure and configure the routes and middleware accordingly.

For a detailed guide on organizing your project's directory structure and configuring routes and middleware using Express Tree Routing Middleware, please refer to the documentation.

Contributing

Contributions are welcome! Please feel free to submit any bug fixes, enhancements, or feature requests via GitHub issues and pull requests.

0.0.8

6 months ago

0.0.7

9 months ago

0.0.6

9 months ago

0.0.5

10 months ago

0.0.4

12 months ago

0.0.3

12 months ago

0.0.2

12 months ago

0.0.1

12 months ago