HTTP Path Tree Router
http-hash is an HTTP router for Node.js that organizes routes into a strict path tree structure, enabling resolution independent of definition order. Unlike traditional regex-based routers where route order can affect matching, http-hash segments paths (e.g., `/foo/bar` becomes `foo > bar`) to build a tree, supporting static segments, named parameters (`:param`), and splats (`*`). This approach simplifies reasoning about route precedence. The current stable version is 2.0.1, last published approximately 7 years ago (as of April 2026), indicating a long-abandoned project with infrequent or no further development. It primarily targets CommonJS environments.
Common errors
-
Error: Path conflict: tried to add path '/foo/:bar' but found conflicting route
cause Attempted to add a new route that overlaps or conflicts with an already defined route in the hash table.fixReview your route definitions and ensure each path is unique and does not create an ambiguous match for the router. For example, `/foo/:id` and `/foo/:name` might conflict if not carefully designed with static segments. -
ReferenceError: require is not defined
cause The `http-hash` package uses CommonJS module syntax (`require`), but you are attempting to use it in an ES Module (ESM) context without a transpiler or specific Node.js configuration.fixIf in a CommonJS file, ensure `const HttpHash = require('http-hash');` is used. If in an ESM file, either switch to a CommonJS file, use a bundler that handles CJS-to-ESM conversion, or use an alternative router that natively supports ESM. -
TypeError: HttpHash is not a constructor
cause You are attempting to instantiate `HttpHash` using `new HttpHash()` instead of calling it as a factory function.fixThe library exports a function that acts as a factory for router instances. Call `HttpHash()` directly without the `new` keyword: `const hash = HttpHash();`
Warnings
- breaking Adding a route that conflicts with an existing path will throw an exception. This includes conflicting variable names or malformed splat routes (e.g., '*' not at the end).
- gotcha This package is CommonJS-only. Attempting to `import` it directly in a native ES Module (ESM) environment will result in errors unless transpiled by a bundler or run in Node.js with specific compatibility flags.
- gotcha Trailing slashes generally do not matter for matching, but variables and splats will not match empty strings. A splat value does not include the leading slash as it is consumed by the parent node.
- deprecated The package has not been updated in approximately 7 years (last published December 2018). While functional, it is considered abandoned and may not receive future updates, bug fixes, or security patches.
Install
-
npm install http-hash -
yarn add http-hash -
pnpm add http-hash
Imports
- HttpHash
import HttpHash from 'http-hash';
const HttpHash = require('http-hash'); - hash.set
hash.set('/path/:param', handler, { order: 1 });hash.set('/path/:param', handler); - hash.get
const handler = hash.get('/path/value').handler();const route = hash.get('/path/value');
Quickstart
const HttpHash = require('http-hash');
// Create a new http hash instance
const hash = HttpHash();
// Define a route with a named parameter
hash.set('/test/:foo/', (req, res) => {
res.end(`Hello from /test/${req.params.foo}`);
});
// Define a route with a splat parameter
hash.set('/files/*', (req, res) => {
res.end(`Serving file: ${req.splat}`);
});
// Get and use a valid route
let route1 = hash.get('/test/variable-value');
console.log('Route 1 Handler:', route1.handler ? 'Found' : 'Not Found');
console.log('Route 1 Params:', route1.params); // { foo: 'variable-value' }
// Get and use a splat route
let route2 = hash.get('/files/path/to/document.txt');
console.log('Route 2 Handler:', route2.handler ? 'Found' : 'Not Found');
console.log('Route 2 Splat:', route2.splat); // path/to/document.txt
// Get an invalid route
let missingRoute = hash.get('/non-existent');
console.log('Missing Route Handler:', missingRoute.handler ? 'Found' : 'Not Found'); // Not Found