# mini-routerjs

> A basic router concentrating on route parsing and callback registration.

Latest version **1.1.5** (published 2017-03-13) · MIT license · 0 weekly downloads

## Install

```sh
npm install mini-routerjs
pnpm add mini-routerjs
yarn add mini-routerjs
bun add mini-routerjs
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.5 |
| Published | 2017-03-13 |
| First published | 2016-10-27 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Jimmy-YMJ |
| Maintainers | jimmy-ymj |
| Keywords | route parsing, router |

## Links

- npm: https://www.npmjs.com/package/mini-routerjs
- Repository: https://github.com/Jimmy-YMJ/mini-routerjs
- Homepage: https://github.com/Jimmy-YMJ/mini-routerjs#readme
- Issues: https://github.com/Jimmy-YMJ/mini-routerjs/issues
- npm.io page: https://npm.io/package/mini-routerjs

## Dependencies (1)

- [simple-url](https://npm.io/package/simple-url.md) ^1.1.7

## 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

- 1.1.5 (latest) — 2017-03-13
- 1.1.3 — 2016-11-30
- 1.1.2 — 2016-11-14
- 1.1.1 — 2016-10-28
- 1.1.0 — 2016-10-28
- 1.0.1 — 2016-10-28
- 1.0.0 — 2016-10-27

## README

# mini-routerjs
A very basic router with functionalities of parsing route and registering callbacks for browser and nodejs.

## Installing
Use via npm:
```bash
npm install mini-routerjs
```
```javascript
var Router = require('mini-routerjs');

// Use es6 import
import Router from 'mini-routerjs';
```
Use in browser:

Scripts for browser is under [build](https://github.com/Jimmy-YMJ/mini-routerjs/tree/master/build) directory, use `router.js` for development (contains inline source maps) or use `router.min.js` for production. The reference in browser is `window.MiniRouter`.

## Examples

Create a common route:
```javascript
var Router = require('mini-routerjs'),
    router = new Router();

router.create('/foo/:name/bar', function(request){
  console.log(request);
});

router.match('http://hostname/foo/jimmy/bar', 'some data');
/**
* Console output:
*{ url: 
*   { protocol: 'http',
*     auth: '',
*     host: 'hostname',
*     pathname: '/foo/jimmy/bar',
*     query: {},
*     hash: '' },
*  params: { name: 'jimmy' },
*  data: 'some data' }
*/

router.match('/foo/jimmy/bar?foo=bar', 'some data');
/**
* Console output:
*{ url: 
*   { protocol: '',
*     auth: '',
*     host: '',
*     pathname: '/foo/jimmy/bar',
*     query: {foo: 'bar'},
*     hash: '' },
*  params: { name: 'jimmy' },
*  data: 'some data' }
*/
```

Create a mismatch route:
```javascript
var router = require('mini-routerjs'),
  router = new Router();
  
router.createMismatch(function(request){
  console.log(request);
});

router.match('/no-match', 'some data');
/**
* Console output:
*{ url: 
*   { protocol: '',
*     auth: '',
*     host: '',
*     pathname: '/no-match',
*     query: {},
*     hash: '' },
*  params: null,
*  data: 'some data' }
*/
```
## Constructor and methods

## Router(options)
| **Option** | **Type** | **Description** |
| --- | --- | --- |
| strict | `Boolean` | Enable strict routing. `false` by default, '/foo' and '/foo/' are treated the same by the router. |
| parseQuery | `Boolean` | Whether parse the query string to object, default `true`. |

### Router.create(route, callback, strict)

| **Params** | **Type** | **Description** |
| --- | --- | --- |
| route | `String` |  The route pattern. |
| callback | `Function` | The callback which will be called when the route is matched, default `function(){}` |
| strict | `Boolean` | This will override the constructor's strict option. |

When a **route** is matched, the corresponding **callback** will be passed a **request** param and executed. The **request** param has a structure like:
```javascript
{
  url: url, // A parsed url object which matchs this route.
  params: params, // The params extracted from url.
  data: data // The data passed by match method.
}
```
Url is parsed by [simple-url](https://github.com/Jimmy-YMJ/simple-url).

The **route** support following patterns:
- `*` will match any none '/' characters no greedy, the matched part will be indexed as the order in all `*` and `**`.
- `**` will match any characters, the matched part will be indexed as the order of in all `*` and `**`.
- `:name` will match a part of none '/' characters, the matched part will be indexed as 'name'.
- `()` will make the part between it optional.

| **Route** | **url** | **params** |
| --- | --- | --- |
| `/foo/*.gif` | `/foo/hello.gif` | `{'0': 'hello'}` |
| `/foo/**` | `/foo/bar/hello` | `{'0': 'bar/hello'}` |
| `/foo/**/*.gif` | `/foo/bar/hello.gif` | `{'0': 'bar', '1': 'hello'}` |
| `/foo/:bar/hello` | `/foo/bar/hello` | `{'bar': 'bar'}` |
| `/foo/:bar/*.gif` | `/foo/bar/hello.gif` | `{'bar': 'bar', '0': 'hello'}` |
| `/foo/(:bar/hello)` | `/foo/bar/hello` | `{'bar': 'bar'}` |
| `/foo/(:bar/hello)` | `/foo/` | `{'bar': undefined}` |

### Router.match(url, data)
Do a match with **url** and pass **data** to callback.

### Router.createMissmatch(callback);
Create a missmatch, the **callback** will be called with a **request** param when any match is failed.

## License
MIT

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