{"id":16898,"library":"server-router","title":"Server Router for Streaming HTTP","description":"server-router is a Node.js HTTP router designed for streaming servers, leveraging a radix-trie data structure for high-performance route matching. It provides a concise API for defining routes with HTTP methods, including support for route parameters and wildcard paths. The current stable version is 6.1.0. Given its \"experimental\" stability rating as indicated by its documentation, its release cadence is irregular and breaking changes are possible without strict adherence to semantic versioning. It differentiates itself by focusing on server-side streaming applications and efficient routing, contrasting with client-side alternatives like nanorouter or more general-purpose routing solutions, by offering a low-level, performant foundation. It is suitable for applications where minimal overhead and direct HTTP stream handling are critical.","status":"active","version":"6.1.0","language":"javascript","source_language":"en","source_url":"https://github.com/yoshuawuyts/server-router","tags":["javascript","server","router","trie","wayfarer"],"install":[{"cmd":"npm install server-router","lang":"bash","label":"npm"},{"cmd":"yarn add server-router","lang":"bash","label":"yarn"},{"cmd":"pnpm add server-router","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Package primarily uses CommonJS `require` syntax. Direct ESM imports are not supported without transpilation or a wrapper.","wrong":"import serverRouter from 'server-router'","symbol":"serverRouter","correct":"const serverRouter = require('server-router')"}],"quickstart":{"code":"var serverRouter = require('server-router')\nvar http = require('http')\n\nvar router = serverRouter({ default: '/404' })\n\nrouter.route('GET', '/hello', function (req, res, params) {\n  res.end('hello world')\n})\n\nrouter.route('PUT', '/hello/:name', function (req, res, params) {\n  res.end('hi there ' + params.name)\n})\n\nrouter.route(['GET', 'POST'], '/json', function (req, res, params) {\n  res.setHeader('Content-Type', 'application/json')\n  res.end(JSON.stringify({ message: 'Dynamic JSON response', params }))\n})\n\nrouter.route('', '/404', function (req, res, params) {\n  res.statusCode = 404\n  res.end('404 Not Found')\n})\n\nconst server = http.createServer(router.start())\nconst PORT = process.env.PORT ?? 3000\n\nserver.listen(PORT, () => {\n  console.log(`Server listening on http://localhost:${PORT}`)\n  console.log('Try visiting: http://localhost:3000/hello')\n  console.log('Try visiting: http://localhost:3000/hello/Alice (e.g., via curl -X PUT)')\n  console.log('Try visiting: http://localhost:3000/json')\n  console.log('Try visiting: http://localhost:3000/nonexistent')\n})","lang":"javascript","description":"Demonstrates setting up a basic HTTP server with multiple routes using GET, PUT, and combined methods, including a 404 handler and dynamic parameter extraction."},"warnings":[{"fix":"Monitor GitHub repository and npm releases for potential changes when upgrading. Consider locking to specific versions or thorough testing before deploying to production.","message":"The package is currently marked as 'experimental' stability. This implies that the API may change without strict adherence to semantic versioning, and breaking changes can occur even in minor or patch releases.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Always use `res.statusCode = <CODE>` to set the HTTP status, e.g., `res.statusCode = 404;`.","message":"The README example for setting a 404 status (`res.status = 404`) is incorrect for Node.js's native `http.ServerResponse` object. The correct property to set the HTTP status code is `res.statusCode`.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Ensure your project uses `require()` for importing `server-router` or configure a build step (e.g., with Babel or TypeScript) to transpile ESM imports to CJS.","message":"This package is designed for CommonJS (CJS) environments and uses `require()`. While Node.js has ESM support, `server-router` does not officially support direct `import` statements without a transpilation step.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Run `npm install server-router` or `yarn add server-router` in your project directory.","cause":"The 'server-router' package has not been installed or is not resolvable in the current project's `node_modules`.","error":"Cannot find module 'server-router'"},{"fix":"Change `res.status = 404` to `res.statusCode = 404`.","cause":"Attempting to set the HTTP status code using `res.status = <CODE>` instead of the correct `res.statusCode = <CODE>` on a native Node.js `http.ServerResponse` object.","error":"TypeError: res.status is not a function"}],"ecosystem":"npm","meta_description":null}