biscuit-route v1.0.0
BiscuitRouter - A High-Performance Node.js Router
BiscuitRouter is a lightweight, high-performance router designed for Node.js applications. It provides an efficient Radix tree-based routing system, built-in middleware support, and an optional LRU cache for faster route lookups.
Features
Radix Tree Routing: Efficiently handles static, dynamic, and wildcard routes.
Middleware Composition: Supports middleware functions with req, res, next structure.
LRU Caching: Speeds up route resolution for frequently accessed paths.
Route Groups: Organize routes with common prefixes.
Optimized Performance: Uses low-level optimizations to minimize resource usage.
Installation
npm install biscuit-route
Usage
- Creating a Router Instance
const { BiscuitRouter } = require('./BiscuitRouter');
const router = new BiscuitRouter({ cacheSize: 5000 });
- Defining Routes
router.on('GET', '/users', (req, res) => {
res.end('List of users');
});
router.on('POST', '/users', (req, res) => {
res.end('User created');
});
- Using Middleware
router.use((req, res, next) => {
console.log(`Incoming request: ${req.method} ${req.url}`);
next();
});
router.on('GET', '/profile', (req, res) => {
res.end('User profile');
});
- Using Route Groups
const userRoutes = new RouteGroup(router, '/users');
userRoutes.on('GET', '/', (req, res) => {
res.end('All users');
});
userRoutes.on('GET', '/:id', (req, res) => {
res.end(`User ID: ${req.params.id}`);
});
5. Integrating with an HTTP Server
const http = require('http');
const server = http.createServer((req, res) => {
const handler = router.routes();
handler(req, res, () => {
res.statusCode = 404;
res.end('Not Found');
});
});
server.listen(3000, () => console.log('Server running on http://localhost:3000'));
API Reference
new BiscuitRouter(options)
Creates a new router instance.
Options:
cacheSize (Number) - Maximum LRU cache size (default: 10000).
ignoreTrailingSlash (Boolean) - Whether to ignore trailing slashes in routes.
router.on(method, path, ...handlers)
Registers a route.
Parameters:
method (String) - HTTP method (e.g., 'GET', 'POST').
path (String) - Route path (supports dynamic /:param and wildcard /*).
handlers (Function) - Middleware and request handlers.
router.use(...middlewares)
Registers global middleware.
Example:
router.use((req, res, next) => {
console.log('Middleware triggered');
next();
});
router.find(method, path)
Finds a route and returns its handlers and parameters.
Example:
const result = router.find('GET', '/users/123');
console.log(result.params); // { id: '123' }
router.routes()
Returns a middleware function compatible with frameworks like Express.
Example:
const app = require('express')();
app.use(router.routes());
new RouteGroup(router, prefix)
Creates a group of routes with a common prefix.
Example:
const group = new RouteGroup(router, '/admin');
group.on('GET', '/dashboard', (req, res) => {
res.end('Admin Dashboard');
});
Benchmark
BiscuitRouter is optimized for speed and low memory usage. It outperforms traditional routers like Express by leveraging Radix trees and LRU caching.
To benchmark:
npx autocannon -c 100 -d 10 http://localhost:3000/users
License
MIT License.
This README provides an overview of BiscuitRouter with examples, API documentation, and performance considerations. Let me know if you need further improvements!
6 months ago