
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
route-parser-ts
Advanced tools
A TypeScript URL routing library with support for named parameters, splats, and optional segments
A TypeScript URL routing library that parses route patterns and matches them against URL paths. Supports named parameters, splat (wildcard) parameters, and optional segments.
Based on route-parser by Ryan Sorensen.
npm install route-parser-ts
import Route from 'route-parser-ts';
// Create a route
const route = Route('/users/:id');
// Match a path
const result = route.match('/users/123');
// => { id: '123' }
// Reverse a route
const path = route.reverse({ id: '456' });
// => '/users/456'
Match exact paths:
const route = Route('/foo');
route.match('/foo'); // => {}
route.match('/foo?query'); // => {} (query strings are ignored)
route.match('/bar'); // => false
route.match('/foobar'); // => false
Capture path segments with :param syntax:
const route = Route('/users/:id');
route.match('/users/123'); // => { id: '123' }
route.match('/users/abc'); // => { id: 'abc' }
route.match('/users/'); // => false
Multiple parameters:
const route = Route('/users/:userId/posts/:postId');
route.match('/users/1/posts/42');
// => { userId: '1', postId: '42' }
Capture multiple path segments with *param syntax:
const route = Route('/files/*path');
route.match('/files/images/photo.jpg');
// => { path: 'images/photo.jpg' }
Multiple splats:
const route = Route('/*a/foo/*b');
route.match('/zoo/woo/foo/bar/baz');
// => { a: 'zoo/woo', b: 'bar/baz' }
Combine splats and named parameters:
const route = Route('/books/*section/:title');
route.match('/books/some/section/last-words-a-memoir');
// => { section: 'some/section', title: 'last-words-a-memoir' }
Make parts of the route optional with parentheses:
const route = Route('/users/:id(/style/:style)');
route.match('/users/3');
// => { id: '3', style: undefined }
route.match('/users/3/style/pirate');
// => { id: '3', style: 'pirate' }
Optional segments starting with a word:
const route = Route('/things/(option/:first)');
route.match('/things/option/bar');
// => { first: 'bar' }
Nested optional segments:
const route = Route('/users/:id(/style/:style(/more/:param))');
route.match('/users/3');
// => { id: '3', style: undefined, param: undefined }
route.match('/users/3/style/pirate');
// => { id: '3', style: 'pirate', param: undefined }
route.match('/users/3/style/pirate/more/things');
// => { id: '3', style: 'pirate', param: 'things' }
Route(spec: string)Creates a new route parser. Can be called with or without new.
const route1 = Route('/users/:id');
const route2 = new Route('/users/:id');
Throws an error if spec is not provided.
route.match(path: string): object | falseMatches a path against the route pattern. Returns an object with the captured parameters on success, or false if the path doesn't match.
const route = Route('/users/:id');
route.match('/users/123'); // => { id: '123' }
route.match('/posts/123'); // => false
route.reverse(params?: object): string | falseGenerates a path from the route pattern and provided parameters. Returns the path string on success, or false if required parameters are missing.
const route = Route('/users/:id/posts/:postId');
route.reverse({ id: '1', postId: '42' });
// => '/users/1/posts/42'
route.reverse({ id: '1' });
// => false (missing required parameter)
With optional segments:
const route = Route('/things/(option/:first)');
route.reverse({ first: 'bar' });
// => '/things/option/bar'
route.reverse();
// => '/things/' (optional segment omitted)
Falsy values (like 0) are handled correctly:
const route = Route('/items/:id?page=:page');
route.reverse({ id: 1, page: 0 });
// => '/items/1?page=0'
The library is written in TypeScript and includes type definitions.
import Route, { RouteParams } from 'route-parser-ts';
const route = Route('/users/:id');
const params: RouteParams | false = route.match('/users/123');
if (params) {
console.log(params.id); // '123'
}
This library is a TypeScript reimplementation of route-parser by Ryan Sorensen. The original library provides the same functionality in JavaScript.
MIT
FAQs
A TypeScript URL routing library with support for named parameters, splats, and optional segments
We found that route-parser-ts demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.