npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

express-tree-routing

v0.0.8

Published

An express-js middleware that creates routing addresses for applications using the tree structure of their parent directories.

Downloads

4

Readme

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.