{"id":11960,"library":"rou3","title":"rou3 Router","description":"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.","status":"active","version":"0.8.1","language":"javascript","source_language":"en","source_url":"https://github.com/h3js/rou3","tags":["javascript","typescript"],"install":[{"cmd":"npm install rou3","lang":"bash","label":"npm"},{"cmd":"yarn add rou3","lang":"bash","label":"yarn"},{"cmd":"pnpm add rou3","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"rou3 is primarily designed for ESM environments, though CommonJS usage might be possible via bundlers.","wrong":"const { createRouter } = require('rou3');","symbol":"createRouter","correct":"import { createRouter } from 'rou3';"},{"note":"All public API functions are named exports. There is no default export.","wrong":"import addRoute from 'rou3';","symbol":"addRoute","correct":"import { addRoute } from 'rou3';"},{"note":"Used to match an incoming path and method against registered routes.","symbol":"findRoute","correct":"import { findRoute } from 'rou3';"},{"note":"TypeScript type for extracting route parameters, added in v0.7.6.","symbol":"InferRouteParams","correct":"import type { InferRouteParams } from 'rou3';"}],"quickstart":{"code":"import { createRouter, addRoute, findRoute } from 'rou3';\n\nconst router = createRouter();\n\n// Add various route types\naddRoute(router, 'GET', '/users', { handler: 'listUsers' });\naddRoute(router, 'POST', '/users/:id', { handler: 'createUser' });\naddRoute(router, 'GET', '/posts/:category/:slug', { handler: 'viewPost' });\naddRoute(router, 'GET', '/files/**:path', { handler: 'serveStatic' });\n\n// Match routes and extract parameters\nconsole.log('GET /users:', findRoute(router, 'GET', '/users'));\n// Expected: { handler: 'listUsers' }\n\nconsole.log('POST /users/123:', findRoute(router, 'POST', '/users/123'));\n// Expected: { handler: 'createUser', params: { id: '123' } }\n\nconsole.log('GET /posts/tech/my-article:', findRoute(router, 'GET', '/posts/tech/my-article'));\n// Expected: { handler: 'viewPost', params: { category: 'tech', slug: 'my-article' } }\n\nconsole.log('GET /files/images/logo.png:', findRoute(router, 'GET', '/files/images/logo.png'));\n// Expected: { handler: 'serveStatic', params: { path: 'images/logo.png' } }\n\nconsole.log('GET /not-found:', findRoute(router, 'GET', '/not-found'));\n// Expected: undefined","lang":"typescript","description":"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."},"warnings":[{"fix":"Review your route patterns against the new URLPattern-compatible syntax documentation. Test all existing routes thoroughly after upgrading.","message":"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.","severity":"breaking","affected_versions":">=0.8.0"},{"fix":"Always prepend a '/' to your route paths, e.g., `/users` instead of `users`.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Ensure all HTTP methods are uppercase, e.g., 'GET', 'POST', 'PUT', 'DELETE'.","message":"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.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"For literal characters, use `\\:` for a colon and `\\*` for an asterisk. For example, `/static\\:path/`.","message":"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 (`\\`).","severity":"gotcha","affected_versions":">=0.7.12"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure 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.","cause":"Attempting to use `addRoute` without correctly importing it from an ESM module, likely in a CommonJS context or with incorrect import syntax.","error":"TypeError: addRoute is not a function"},{"fix":"Verify 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.","cause":"Common causes include paths not starting with `/`, methods not being uppercase, or incorrect URL pattern syntax, especially after v0.8.0 compatibility changes.","error":"Router does not match expected path/method combinations, returns `undefined`"}],"ecosystem":"npm"}