rou3 Router
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
-
TypeError: addRoute is not a function
cause Attempting to use `addRoute` without correctly importing it from an ESM module, likely in a CommonJS context or with incorrect import syntax.fixEnsure you are using `import { addRoute } from 'rou3';` in an ESM environment, or configure your bundler (like Webpack/Rollup/esbuild) to correctly handle ESM modules in a CommonJS project. -
Router does not match expected path/method combinations, returns `undefined`
cause Common causes include paths not starting with `/`, methods not being uppercase, or incorrect URL pattern syntax, especially after v0.8.0 compatibility changes.fixVerify that all paths start with `/` and all methods are uppercase. Check your route patterns against the URLPattern compatibility documentation, particularly for complex regex or wildcard usage.
Warnings
- breaking Version 0.8.0 introduced significant changes to URL pattern compatibility. While aiming for URLPattern API syntax, existing complex patterns might behave differently or require adjustments.
- gotcha All paths added to rou3 must begin with a forward slash `/`. Paths like `users` (without a leading slash) will not be correctly registered or matched.
- gotcha HTTP methods passed to `addRoute` and `findRoute` must be in UPPERCASE. Using lowercase methods (e.g., 'get' instead of 'GET') will result in routes not being matched.
- gotcha If a route pattern contains literal colons (`:`) or asterisks (`*`) that are not intended to be parameters or wildcards, they must be escaped with a backslash (`\`).
Install
-
npm install rou3 -
yarn add rou3 -
pnpm add rou3
Imports
- createRouter
const { createRouter } = require('rou3');import { createRouter } from 'rou3'; - addRoute
import addRoute from 'rou3';
import { addRoute } from 'rou3'; - findRoute
import { findRoute } from 'rou3'; - InferRouteParams
import type { InferRouteParams } from 'rou3';
Quickstart
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