Server Router for Streaming HTTP
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.
Common errors
-
Cannot find module 'server-router'
cause The 'server-router' package has not been installed or is not resolvable in the current project's `node_modules`.fixRun `npm install server-router` or `yarn add server-router` in your project directory. -
TypeError: res.status is not a function
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.fixChange `res.status = 404` to `res.statusCode = 404`.
Warnings
- breaking 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.
- gotcha 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`.
- gotcha 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.
Install
-
npm install server-router -
yarn add server-router -
pnpm add server-router
Imports
- serverRouter
import serverRouter from 'server-router'
const serverRouter = require('server-router')
Quickstart
var serverRouter = require('server-router')
var http = require('http')
var router = serverRouter({ default: '/404' })
router.route('GET', '/hello', function (req, res, params) {
res.end('hello world')
})
router.route('PUT', '/hello/:name', function (req, res, params) {
res.end('hi there ' + params.name)
})
router.route(['GET', 'POST'], '/json', function (req, res, params) {
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify({ message: 'Dynamic JSON response', params }))
})
router.route('', '/404', function (req, res, params) {
res.statusCode = 404
res.end('404 Not Found')
})
const server = http.createServer(router.start())
const PORT = process.env.PORT ?? 3000
server.listen(PORT, () => {
console.log(`Server listening on http://localhost:${PORT}`)
console.log('Try visiting: http://localhost:3000/hello')
console.log('Try visiting: http://localhost:3000/hello/Alice (e.g., via curl -X PUT)')
console.log('Try visiting: http://localhost:3000/json')
console.log('Try visiting: http://localhost:3000/nonexistent')
})