Registry / http-networking / router-http

router-http

JSON →
library2.0.6jsnpmunverified

router-http is a lightweight (1.3 kB min+gzipped) and performant HTTP router for Node.js, currently at stable version 2.0.6. It aims to provide an Express-like middleware API while offering significantly better and more predictable performance than Express's regex-based router. Unlike Express, router-http utilizes a trie-based routing algorithm, specifically find-my-way, which guarantees near O(1) lookup time regardless of the number of registered routes. This makes it particularly suitable for applications with a large number of routes where consistent performance is critical. The package is actively maintained with frequent dependency updates and recent major version releases, supporting Node.js version 18 and above.

npm install router-http
INSTALL
IMPORT
SIG · ROUTER-HTTP
R
router-http
http-networkingjavascriptv2.0.6
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

createRouter
const createRouter = require('router-http')
import createRouter from 'router-http'
router-http is a CommonJS module. ESM import syntax is not supported directly for the package entry point. Use `require()`.
router.get
router.get('/', (req, res) => { /* ... */ })
import { get } from 'router-http'; get('/', (req, res) => { /* ... */ })
Methods like `.get()`, `.post()`, `.use()` are called on the router instance returned by `createRouter`, not directly imported.
MiddlewareFunction
router.use((req, res, next) => { /* ... */ next() })
router.use(async (req, res) => { /* ... */ })
Middleware functions must accept `(req, res, next)` and explicitly call `next()` to pass control to the next middleware or route handler. Omitting `next()` or failing to call it will halt the request processing.

Demonstrates how to initialize router-http, define a final error/404 handler, add global middleware, declare various HTTP method routes with dynamic parameters, and start a basic Node.js HTTP server.

const http = require('http') const createRouter = require('router-http') const finalHandler = (error, req, res) => { if (error) { res.statusCode = error.statusCode || 500 res.end(error.message) } else { res.statusCode = 404 res.end('Not Found') } } const router = createRouter(finalHandler, { caseSensitive: false, ignoreTrailingSlash: true }) // Global middleware (runs on every request) router.use((req, res, next) => { req.timestamp = Date.now() next() }) router .get('/', (req, res) => res.end(`Hello World at ${req.timestamp}`)) .post('/users', (req, res) => res.end('User created')) .put('/users/:id', (req, res) => res.end(`User ${req.params.id} updated`)) .delete('/users/:id', (req, res) => res.end(`User ${req.params.id} deleted`)) .all('/ping', (req, res) => res.end('pong')) const server = http.createServer((req, res) => { router(req, res) }) server.listen(3000, () => { console.log('Server listening on http://localhost:3000') console.log('Try visiting / or /users/123, or POST to /users') })
Debug
Known issues
breakingVersion 2.0.0 introduced a fundamental shift from a likely regex-based routing engine to a trie-based implementation via `find-my-way`. While the external API aims for compatibility, this change can lead to subtle behavioral differences in route matching, parameter parsing, or route prioritization, especially in complex routing scenarios or edge cases, potentially breaking assumptions made on previous versions.
fix
Thoroughly test existing routing logic and middleware behavior after upgrading to v2.x, paying close attention to dynamic parameters, optional segments, and conflicting routes. Consult the `find-my-way` documentation for specific routing behaviors.
affects: >=2.0.0
gotcharouter-http is a CommonJS module, meaning `import` syntax is not natively supported for the package itself without transpilation or specific Node.js configuration (`"type": "module"` in your project's `package.json` and a default CommonJS module would cause issues). The `require()` syntax must be used to load the `createRouter` function.
fix
Always use `const createRouter = require('router-http')` to import the module in a CommonJS environment. If you are developing an ESM project, you might need to use dynamic `import('router-http')` or a build tool that handles CJS interoperability.
affects: >=1.0.0
gotchaMiddleware functions require an explicit `next()` call to pass control to subsequent middleware or the final route handler. Failing to call `next()` will cause the request to hang unless `res.end()` or `res.statusCode` is explicitly set within the middleware.
fix
Ensure all middleware functions follow the `(req, res, next)` signature and include `next()` at the point where control should be passed. For asynchronous operations, call `next()` after the operation completes or if an error occurs (`next(error)`).
affects: >=1.0.0
breakingThe package explicitly requires Node.js version 18 or higher. Running on older Node.js environments will result in errors or unexpected behavior due to unsupported language features or API changes.
fix
Upgrade your Node.js environment to version 18 or newer to meet the minimum engine requirements. Check your `package.json` `engines` field and ensure your deployment environment is compatible.
affects: <18.0.0
Errors
Common errors & fixes
TypeError: next is not a function
A middleware function was declared without the `next` argument or `next()` was called on a non-function.
fix
Ensure your middleware functions are defined with `(req, res, next)` as parameters, and `next()` is correctly invoked. Example: `router.use((req, res, next) => { /* ... */ next() })`.
Cannot GET /some/path (or similar HTTP 404 Not Found response)
The requested URL path does not match any defined route, or route matching options (case sensitivity, trailing slashes) are causing a mismatch.
fix
Verify that the route path is correctly defined and matches the incoming request, considering case sensitivity and trailing slashes. Check the `createRouter` options for `caseSensitive` and `ignoreTrailingSlash`. Example: `createRouter(finalHandler, { caseSensitive: false, ignoreTrailingSlash: true })`.
TypeError: Cannot read properties of undefined (reading 'id') for req.params.id
Attempting to access `req.params` for a route that does not have dynamic segments, or the dynamic segment name in the route definition doesn't match the one accessed in `req.params`.
fix
Ensure your route is defined with dynamic segments (e.g., `/users/:id`) if you intend to access `req.params`. Double-check that the parameter name used in `req.params.name` matches the `:name` in your route definition.
Upgrade
Version history
2.0.6latest on npm
Audit
Dependencies
find-my-wayrequiredCore routing engine, providing the trie-based algorithm for predictable performance.
Agent activity
12 hits · last 30 days
node
10
OpenAI (training)
1
Resources