rou3 Router

0.8.1 · active · verified Sun Apr 19

rou3 is a lightweight and fast JavaScript router, currently at version 0.8.1. It provides efficient routing capabilities for various JavaScript environments including Node.js, Bun, Deno, and browsers. The library is actively maintained with frequent minor releases and bug fixes, often including performance enhancements and type improvements, as seen in recent versions like 0.8.0 and 0.7.12. Key differentiators include its URLPattern-compatible syntax for defining routes, support for complex patterns like named wildcards, optional parameters, and custom regex, as well as features for escaping special characters in paths. It ships with TypeScript types, ensuring a robust developer experience for typed applications.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates creating a router, adding various route patterns including parameters and wildcards, and then using `findRoute` to match incoming requests and extract associated data and parameters.

import { createRouter, addRoute, findRoute } from 'rou3';

const router = createRouter();

// Add various route types
addRoute(router, 'GET', '/users', { handler: 'listUsers' });
addRoute(router, 'POST', '/users/:id', { handler: 'createUser' });
addRoute(router, 'GET', '/posts/:category/:slug', { handler: 'viewPost' });
addRoute(router, 'GET', '/files/**:path', { handler: 'serveStatic' });

// Match routes and extract parameters
console.log('GET /users:', findRoute(router, 'GET', '/users'));
// Expected: { handler: 'listUsers' }

console.log('POST /users/123:', findRoute(router, 'POST', '/users/123'));
// Expected: { handler: 'createUser', params: { id: '123' } }

console.log('GET /posts/tech/my-article:', findRoute(router, 'GET', '/posts/tech/my-article'));
// Expected: { handler: 'viewPost', params: { category: 'tech', slug: 'my-article' } }

console.log('GET /files/images/logo.png:', findRoute(router, 'GET', '/files/images/logo.png'));
// Expected: { handler: 'serveStatic', params: { path: 'images/logo.png' } }

console.log('GET /not-found:', findRoute(router, 'GET', '/not-found'));
// Expected: undefined

view raw JSON →