{"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.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install router-http"],"cli":null},"imports":["const createRouter = require('router-http')","router.get('/', (req, res) => { /* ... */ })","router.use((req, res, next) => { /* ... */ next() })"],"auth":{"required":false,"env_vars":[]},"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.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}