Registry / web-framework / route-recognizer

route-recognizer

JSON →
library0.3.4jsnpmunverified

route-recognizer is a compact JavaScript library (under 2KB) designed solely for matching paths against a set of registered routes. It functions as a low-level primitive for more comprehensive routing systems, such as `router.js`, which was notably used by Ember.js. Currently at version 0.3.4, last published in 2018, the library offers stable functionality for static, dynamic (e.g., `:id`), and wildcard (e.g., `*path`) segments. Its core differentiator is its minimalistic approach, focusing exclusively on route recognition and parameter extraction without handling routing logic, history, or view rendering. The package ships with TypeScript types, but its development status appears to be unmaintained given the last commit and release dates.

npm install route-recognizer
INSTALL
IMPORT
SIG · ROUTE-RECOGNIZER
R
route-recognizer
web-frameworkjavascriptv0.3.4
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

RouteRecognizer
✓ import RouteRecognizer from 'route-recognizer';
✗ import { RouteRecognizer } from 'route-recognizer';
The library uses a default export for its main class, not named export for the class itself.
RouteRecognizer (CommonJS)
✓ const RouteRecognizer = require('route-recognizer');
✗ const { RouteRecognizer } = require('route-recognizer');
CommonJS usage aligns with the default export pattern; directly assigning the require result to the class name.
RouteRecognizer (Type)
✓ import type { RouteRecognizer } from 'route-recognizer';
For TypeScript, import the type explicitly if only type-checking is needed, although direct value import also works for both value and type.

This quickstart demonstrates how to instantiate RouteRecognizer, add various route patterns including dynamic and star segments, and then use the `recognize` method to match paths and retrieve associated handlers and parameters.

import RouteRecognizer from 'route-recognizer'; const router = new RouteRecognizer(); const postsHandler = (params) => `Handling posts with params: ${JSON.stringify(params)}`; const singlePostHandler = (params) => `Handling single post with ID: ${params.id}`; const adminHandler = () => `Admin section accessed`; const pageHandler = (params) => `Serving page: ${params.path}`; router.add([{ path: "/posts", handler: postsHandler }], { as: "allPosts" }); router.add([{ path: "/posts/:id", handler: singlePostHandler }], { as: "singlePost" }); router.add([ { path: "/admin", handler: adminHandler }, { path: "/posts", handler: postsHandler } ], { as: "adminPosts" }); router.add([{ path: "/pages/*path", handler: pageHandler }], { as: "catchAllPages" }); let result = router.recognize("/posts"); console.log("Recognizing /posts:", result?.[0]?.handler(result?.[0]?.params)); result = router.recognize("/posts/123"); console.log("Recognizing /posts/123:", result?.[0]?.handler(result?.[0]?.params)); result = router.recognize("/admin/posts"); console.log("Recognizing /admin/posts:", result?.[1]?.handler(result?.[1]?.params)); result = router.recognize("/pages/about/us"); console.log("Recognizing /pages/about/us:", result?.[0]?.handler(result?.[0]?.params));
Debug
Known issues
gotchaWhen multiple routes match a given path, `route-recognizer` prioritizes routes based on specificity: explicit static paths are preferred over dynamic segments, and fewer dynamic segments are preferred. If segments are identical, the number of handlers and then definition order are used as tiebreakers. This behavior might be unexpected if a user anticipates a different matching priority (e.g., first-defined wins).
fix
Always test routes with ambiguous paths to ensure they resolve as intended. Structure route definitions from most specific to least specific, if possible, to align with the recognizer's internal sorting logic.
affects: >=0.1.0
deprecatedThe `route-recognizer` package has not seen active development since its last release (0.3.4 in 2018), and its GitHub repository shows no recent commits. Development dependencies mentioned in the README (like Bower and Travis CI) are largely outdated. This indicates the library is no longer actively maintained and may not be compatible with newer JavaScript features or build environments without additional polyfills or transpilation.
fix
Consider alternatives for new projects. For existing projects, be aware that security updates or bug fixes are unlikely. Test thoroughly in modern environments.
affects: >=0.3.4
gotchaDynamic segments (e.g., `:id`) in `route-recognizer` match any character *except* a forward slash (`/`). This means a dynamic segment will not consume subsequent path segments. For matching multiple path segments, a star segment (e.g., `*path`) should be used instead.
fix
Use dynamic segments (`:param`) for single path segments and star segments (`*param`) for multi-segment or trailing path matches.
affects: >=0.1.0
Errors
Common errors & fixes
ReferenceError: RouteRecognizer is not defined
The `RouteRecognizer` class was not correctly imported or required before use.
fix
For ES Modules: `import RouteRecognizer from 'route-recognizer';` For CommonJS: `const RouteRecognizer = require('route-recognizer');`
TypeError: router.add is not a function
The `router` variable is not an instance of `RouteRecognizer`. This usually happens if `new` keyword is omitted during instantiation.
fix
Ensure `RouteRecognizer` is instantiated correctly: `const router = new RouteRecognizer();`
Error: There are no routes named `routeName`
This error occurs when attempting to generate a URL by name (if the library supported it, which `route-recognizer` doesn't directly expose but a higher-level router built on top might) or if a named route was not registered. `route-recognizer` itself does not have a public API for generating paths by name; names are for identification within its internal structure or for systems built on top. The core library only recognizes paths.
fix
This specific error is unlikely to come directly from `route-recognizer` as it primarily deals with `recognize`. If encountered in a surrounding router, ensure the route name used for generation matches a registered route. For `route-recognizer` directly, verify that you are not trying to generate routes by name, but rather using `recognize` with a path string.
Upgrade
Version history
0.3.4latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
route-recognizer — npm install route-recognizer · libregistry