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

@xiliris/express-route-handler

v2.0.2

Published

Dynamic Express Route Handler is a utility for Express.js applications that automatically loads routes from a specified directory.

Downloads

5

Readme

Express Route Handler

Dynamic Express Route Handler is a utility for Express.js applications that automatically loads routes from a specified directory.

Features

  • Automatically loads all .js files in a specified directory and its subdirectories as routes.
  • Supports nested routes based on the directory structure.
  • Provides logging functionality to track the loading process.
  • Allows the application of middleware to specific routes for enhanced functionality and control.
  • Ignores files starting with an underscore and directories surrounded by parentheses.

Installation

npm install @xiliris/express-route-handler

Contact

https://adnanskopljak.com

Usage:

const express = require("express");
const RouteHandler = require("@xiliris/express-route-handler");
const path = require("path");

const app = express();
const routesDirectory = path.join(__dirname, "routes");
const data = {
  log: true,
};

const routeHandler = new RouteHandler(app, routesDirectory, data);
routeHandler.handleRoutes();

app.listen(3000, () => {
  console.log("Server is running on port 3000");
});

Explanation

new RouteHandler(app, dir, data)

  • app: An instance of an Express application.
  • dir: The path to the directory containing the route files.
  • data: An object with the following properties:

    log: A boolean indicating whether to log the loading process.

Middleware Configuration

  • The middleware property in the data object allows you to specify middleware to be applied to certain routes. Each middleware configuration object should have the following properties:

  • path: The route path where the middleware should be applied.

  • middlewares: An array of middleware functions to be applied.

File Structure

.
├── routes                     # Directory where route files are stored
│   ├── index.js               # Treated as "/"
│   ├── users                  # Treated as a nest, "/users"
│   │   ├── index.js           # Treated as "/users"
│   │   └── [id]               # Treated as a nest, "/users/:id"
│   │       └── index.js       # Treated as "/users/:id"
│   ├── products               # Treated as a nest, "/products"
│   │   ├── index.js           # Treated as "/products"
│   │   ├── [id]               # Treated as a nest, "/products/:id"
│   │   │   └── index.js       # Treated as "/products/:id"
│   │   ├── _archive.js        # Ignored because it starts with an underscore
│   │   └── (old)              # Ignored because it's surrounded by parentheses
│   │       └── old_product.js # Ignored because its parent directory is ignored
│   └── orders                 # Treated as a nest, "/orders"
│       ├── index.js           # Treated as "/orders"
│       └── [id]               # Treated as a nest, "/orders/:id"
│           └── index.js       # Treated as "/orders/:id"
├── app.js                     # Main file of your Express application
  • routes is the directory where your route files are stored. It contains index.js, users.js, products, and orders. The products directory contains its own index.js, _archive.js, and (old) directories. The (old) directory is ignored by the route handler because it's surrounded by parentheses. The _archive.js file is also ignored because it starts with an underscore.
  • app.js is the main file of your Express application. This is where you would use the routeHandler function.
  • routes/products/[id]/index.js, and routes/orders/[orderId]/index.js are dynamic routes. The [id] part of the folder name is a placeholder that will be replaced with the actual ID in the URL. For example, if you have a URL like /orders/123, it will be handled by the routes/orders/[id]/index.js route.

NOTE

  • When using dynamic routes (like [id]), it's important to create your router with the { mergeParams: true } option. This ensures that the router has access to the parameters of its parent router. Here's how you can do it:
const express = require("express");
const router = express.Router({ mergeParams: true });
  • Without { mergeParams: true }, req.params in the route handler would not contain the id parameter from the parent router's path.

Example with Middleware

const express = require("express");
const path = require("path");
const RouteHandler = require("@xiliris/express-route-handler");
const isMiddleware = require("./middleware/isMiddleware");
const addon = require("./middleware/addon");

const app = express();
const port = 3000;
const routesDirectory = path.join(__dirname, "routes");
const data = {
  log: true,
  middleware: [
    {
      path: "/users",
      middlewares: [isMiddleware, addon],
    },
  ],
};

const routeHandler = new RouteHandler(app, routesDirectory, data);
routeHandler.handleRoutes();

app.listen(port, () => {
  console.log(`Server listening at http://localhost:${port}`);
});