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 🙏

© 2026 – Pkg Stats / Ryan Hefner

express-router-bds

v1.1.1

Published

High-performance routing system for Express based on the proven insitu-js architecture

Readme

Express-Router

Express-Router is a high-performance routing system for Express.js based on the proven architecture of insitu-js. It provides an optimized routing solution with support for parametrized routes, JSON route files loading, directory-based route loading, and an extensible hooks system.

Key Features

  • High-Performance Routing: Uses indexes and regex caching for optimized route matching
  • Parametrized Routes: Full support for dynamic routes like /users/:id
  • JSON Route Loading: Define routes in JSON configuration files
  • Directory-Based Loading: Load multiple route files from a directory
  • Hook System: Extensible via the express-hooked hook system
  • Express Middleware: Fully compatible with Express.js architecture
  • Duplicate Route Detection: Alerts when duplicate routes are defined
  • Static Route Support: Serve static files from directories

Important Notes on Hook Usage

When using the hook system, be careful with the next parameter in middleware functions:

  • Hooks that receive the next parameter should not call next() unless specifically intended to control flow
  • Calling next() from within a hook can interfere with the normal Express flow and cause issues like "headers already sent" errors
  • For logging or pre-processing purposes, use hooks without calling next()

Example of safe hook usage:

// Safe - only for logging
routerIntegration.addHook('before_route_processing', (req, res) => {
  console.log(`Processing route: ${req.method} ${req.url}`);
});

// Potentially problematic - avoid calling next() unless necessary
routerIntegration.addHook('before_route_processing', (req, res, next) => {
  console.log(`Processing route: ${req.method} ${req.url}`);
  // next(); // Only call if you specifically need to control the flow
});

Installation

npm install express-router-bds express-hooked

Quick Start

1. Basic Setup

const express = require('express');
const ExpressRouterIntegration = require('express-router');

const app = express();
const routerIntegration = new ExpressRouterIntegration();

// Add routes directly
routerIntegration
  .get('/api/hello', (req, res) => {
    res.json({ message: 'Hello from Express-Router!' });
  })
  .get('/api/users/:id', (req, res) => {
    const userId = req.params.id;
    res.json({ message: `User ID: ${userId}` });
  });

// Apply router to Express app
routerIntegration.applyToApp(app);

app.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});

2. JSON Route Files

Define routes in JSON files:

[
  {
    "method": "GET",
    "path": "/api/users",
    "controller": "./controllers/usersController",
    "handler": "listUsers",
    "contentType": "application/json"
  },
  {
    "method": "GET",
    "path": "/api/users/:id",
    "controller": "./controllers/usersController",
    "handler": "getUser",
    "contentType": "application/json"
  }
]

3. Directory-Based Loading

Load routes from multiple JSON files in a directory:

const RouteDirectoryLoader = require('express-router/RouteDirectoryLoader');

const routeDirectoryLoader = new RouteDirectoryLoader();

// Load routes from directory containing multiple JSON files
routeDirectoryLoader.loadRoutesFromDirectory(routerIntegration, './routes')
  .then(routes => {
    console.log(`${routes.length} routes loaded from directory`);
  });

Advanced Features

Hook System

The routing system integrates with the express-hooked hook system, allowing customization through extension points.

Available Hooks

  • pre_route_load: Executed before loading routes from a file
  • post_route_load: Executed after loading routes from a file
  • route_added: Executed after a route is added
  • before_route_processing: Executed before processing any route
  • route_matched: Executed when a matching route is found
  • before_route_handler: Executed before executing a route handler
  • after_route_handler: Executed after executing a route handler
  • route_handler_error: Executed when a route handler error occurs

Custom Hook Example

// Register a hook to execute logic before processing any route
routerIntegration.addHook('before_route_processing', (req, res) => {
  console.log(`Processing route: ${req.method} ${req.url}`);
});

// Register a hook when a matching route is found
routerIntegration.addHook('route_matched', (matchedRoute, req, res) => {
  console.log(`Route found: ${matchedRoute.route.path}`);
  console.log(`Parameters:`, matchedRoute.params);
});

Performance Benefits

Express-Router implements several optimizations that make it faster than traditional Express routing:

  • Regex Caching: Regular expressions for parametrized routes are cached to avoid recompilation
  • Route Indexing: Routes are indexed by method and path segments for faster lookup
  • Pre-filtering: Candidate routes are pre-filtered using indexes before detailed matching
  • Hierarchical Search: Multiple search strategies (exact, parametrized, prefix) with optimal ordering

Why Choose Express-Router?

  • Battle-tested Architecture: Based on the proven insitu-js routing system used in production
  • High Performance: Optimized algorithms for route matching
  • Flexible Loading: Support for both file-based and directory-based route definitions
  • Extensible: Rich hook system for customization
  • Developer Friendly: Clean API and comprehensive documentation
  • Production Ready: Built with reliability and scalability in mind

License

Apache 2.0