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