# url-router

> A Trie-based router

Latest version **13.0.0** (published 2021-01-28) · MIT license · 0 weekly downloads

## Install

```sh
npm install url-router
pnpm add url-router
yarn add url-router
bun add url-router
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 13.0.0 |
| Published | 2021-01-28 |
| First published | 2015-04-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 18.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 10 |
| Author | Jiang Fengming |
| Maintainers | jiangfengming |
| Keywords | url, router, trie |

## Links

- npm: https://www.npmjs.com/package/url-router
- Repository: https://github.com/jiangfengming/url-router
- Issues: https://github.com/jiangfengming/url-router/issues
- npm.io page: https://npm.io/package/url-router

## Alternatives

- [express-promise-router](https://npm.io/package/express-promise-router.md) — 736.1K weekly downloads
- [next-usequerystate](https://npm.io/package/next-usequerystate.md) — 29.8K weekly downloads
- [@bitkyc08/opencodex](https://npm.io/package/@bitkyc08/opencodex.md) — 4.6K weekly downloads
- [lynkr](https://npm.io/package/lynkr.md) — 575 weekly downloads
- [baremetal.js](https://npm.io/package/baremetal.js.md) — 42 weekly downloads

## Recent versions

- 13.0.0 (latest) — 2021-01-28
- 12.0.0 — 2020-12-18
- 11.1.0 — 2020-12-04
- 11.0.4 — 2020-12-02
- 11.0.3 — 2020-03-05
- 11.0.2 — 2020-01-13
- 11.0.1 — 2019-12-12
- 11.0.0 — 2019-11-13
- 10.0.1 — 2019-11-12
- 10.0.0 — 2019-11-12
- 10.0.0-0 — 2019-11-12
- 9.0.1 — 2019-09-04
- 9.0.0 — 2019-09-04
- 8.0.0 — 2019-08-23
- 7.1.5 — 2019-04-15
- … 31 more at https://npm.io/package/url-router/versions

## README

# url-router
A Trie-based router.

## V13 Breaking Change
The constructor's `routes` parameter changed to key-value object that key is pattern and value is handler.

## Installation
```
npm install url-router
```

NOTE: This package is written in ES2020 syntax and not transpiled. It is tested only on Node.js v14 LTS.
To use it in old browsers, you should transpile the code using tools such as Babel.

## Examples

```js
import assert from 'assert';
import Router from 'url-router';

const router = new Router({
  '/foo': 1,
  '/foo/bar': 2,
  '/user/:id': 3,
  '/user/:id/:page': 4,
  '/people/:name(\\w+)': 5,
  '(.*)': 6,
  '/:year(\\d+)-:month(\\d+)': 7
});

assert.deepStrictEqual(
  router.find('/foo'),

  {
    handler: 1,
    params: {}
  }
);

assert.deepStrictEqual(
  router.find('/foo/bar'),

  {
    handler: 2,
    params: {}
  }
);

assert.deepStrictEqual(
  router.find('/user/123'),

  {
    handler: 3,
    params: {
      id: '123'
    }
  }
);

assert.deepStrictEqual(
  router.find('/user/456/articles'),

  {
    handler: 4,
    params: {
      id: '456',
      page: 'articles'
    }
  }
);

assert.deepStrictEqual(
  router.find('/people/john'),

  {
    handler: 5,
    params: {
      name: 'john'
    }
  }
);

assert.deepStrictEqual(
  router.find('/404'),

  {
    handler: 6,
    params: {}
  }
);

assert.deepStrictEqual(
  router.find('/2019-11'),

  {
    handler: 7,
    params: {
      year: '2019',
      month: '11'
    }
  }
);
```

## API

### Router
```js
const routes = {
  pattern_1: handler_1,
  pattern_2: handler_2,
  ...
};

router = new Router(routes);
```

Creates a router instance.

#### Params
##### routes
Optional. A key-value object that key is pattern and value is handler.
See `router.add()` below for how to define pattern and handler.

### router.add
```js
router.add(pattern, handler)
```

Adds a route entry.

#### Params

##### pattern
`String`. The pattern to match against the request path.

You can define params in `pattern`, for example:

```js
const router = new Router();
router.add('/people/:username/:year(\\d+)-:month(\\d+)/:articleId(\\d+)', handler);
const result = router.find('/people/johnsmith/2020-02/123');
/*
result:
{
  handler: handler,
  params: {
    username: 'johnsmith',
    year: '2020',
    month: '02,
    articleId: '123'
  }
}
*/
```

If regex is omitted, it defaults to `[^/]+`.

You can also use regex without setting the parameter name, for example:

```js
router.add('(.*)', NotFound)
```

This defines a catch-all route.

##### handler
`any`. The handler you wish to handle the request.
Based on your framework design, the handler can be a function to handle the request,
or the file path to your controller file, or an object (such as Vue component), etc.

#### Returns
The router instance. So you could use method chaining:

```js
router
  .add('/foo', foo)
  .add('/bar', bar)
```

### router.find
```js
router.find(path)
```

Finds the route which matches the path.

#### Parameters

##### path
`String`. The request path.  

#### Returns

`handler` and `params` of the route:

```js
{
  handler,
  params
}
```

Or `null` if not found.

## License
[MIT](LICENSE)

---
_Source: https://npm.io/package/url-router · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
