{"id":17358,"library":"router-http","title":"Simple HTTP Router","description":"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.","status":"active","version":"2.0.6","language":"javascript","source_language":"en","source_url":"https://github.com/Kikobeats/router-http","tags":["javascript","app","http","middleware","pathname","rest","route","router"],"install":[{"cmd":"npm install router-http","lang":"bash","label":"npm"},{"cmd":"yarn add router-http","lang":"bash","label":"yarn"},{"cmd":"pnpm add router-http","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core routing engine, providing the trie-based algorithm for predictable performance.","package":"find-my-way","optional":false}],"imports":[{"note":"router-http is a CommonJS module. ESM import syntax is not supported directly for the package entry point. Use `require()`.","wrong":"import createRouter from 'router-http'","symbol":"createRouter","correct":"const createRouter = require('router-http')"},{"note":"Methods like `.get()`, `.post()`, `.use()` are called on the router instance returned by `createRouter`, not directly imported.","wrong":"import { get } from 'router-http'; get('/', (req, res) => { /* ... */ })","symbol":"router.get","correct":"router.get('/', (req, res) => { /* ... */ })"},{"note":"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.","wrong":"router.use(async (req, res) => { /* ... */ })","symbol":"MiddlewareFunction","correct":"router.use((req, res, next) => { /* ... */ next() })"}],"quickstart":{"code":"const http = require('http')\nconst createRouter = require('router-http')\n\nconst finalHandler = (error, req, res) => {\n  if (error) {\n    res.statusCode = error.statusCode || 500\n    res.end(error.message)\n  } else {\n    res.statusCode = 404\n    res.end('Not Found')\n  }\n}\n\nconst router = createRouter(finalHandler, { caseSensitive: false, ignoreTrailingSlash: true })\n\n// Global middleware (runs on every request)\nrouter.use((req, res, next) => {\n  req.timestamp = Date.now()\n  next()\n})\n\nrouter\n  .get('/', (req, res) => res.end(`Hello World at ${req.timestamp}`))\n  .post('/users', (req, res) => res.end('User created'))\n  .put('/users/:id', (req, res) => res.end(`User ${req.params.id} updated`))\n  .delete('/users/:id', (req, res) => res.end(`User ${req.params.id} deleted`))\n  .all('/ping', (req, res) => res.end('pong'))\n\nconst server = http.createServer((req, res) => {\n  router(req, res)\n})\n\nserver.listen(3000, () => {\n  console.log('Server listening on http://localhost:3000')\n  console.log('Try visiting / or /users/123, or POST to /users')\n})\n","lang":"javascript","description":"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."},"warnings":[{"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.","message":"Version 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.","severity":"breaking","affected_versions":">=2.0.0"},{"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.","message":"router-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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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)`).","message":"Middleware 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.","severity":"gotcha","affected_versions":">=1.0.0"},{"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.","message":"The 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.","severity":"breaking","affected_versions":"<18.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"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() })`.","cause":"A middleware function was declared without the `next` argument or `next()` was called on a non-function.","error":"TypeError: next is not a function"},{"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 })`.","cause":"The requested URL path does not match any defined route, or route matching options (case sensitivity, trailing slashes) are causing a mismatch.","error":"Cannot GET /some/path (or similar HTTP 404 Not Found response)"},{"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.","cause":"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`.","error":"TypeError: Cannot read properties of undefined (reading 'id') for req.params.id"}],"ecosystem":"npm","meta_description":null}