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-httpVerified import paths — ran on the pinned version, not inferred.
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.
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.
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.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)`).
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.
Ensure your middleware functions are defined with `(req, res, next)` as parameters, and `next()` is correctly invoked. Example: `router.use((req, res, next) => { /* ... */ next() })`.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 })`.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.