# siren-router

> An express-style router middleware for koa.

Latest version **1.0.9** (published 2015-01-29) · MIT license · 0 weekly downloads

## Install

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

## 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.0.9 |
| Published | 2015-01-29 |
| First published | 2015-01-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Maintainers | texvnars |
| Keywords | koa, middleware, router, route |

## Links

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

## Dependencies (5)

- [methods](https://npm.io/package/methods.md) ^1.1.1
- [debuglog](https://npm.io/package/debuglog.md) ^1.0.1
- [statuses](https://npm.io/package/statuses.md) ^1.2.0
- [koa-compose](https://npm.io/package/koa-compose.md) ^2.3.0
- [path-to-regexp](https://npm.io/package/path-to-regexp.md) ^1.0.2

## 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.0.9 (latest) — 2015-01-29
- 1.0.8 — 2015-01-29
- 1.0.7 — 2015-01-19
- 1.0.6 — 2015-01-19
- 1.0.5 — 2015-01-19
- 1.0.4 — 2015-01-16
- 1.0.3 — 2015-01-16
- 1.0.2 — 2015-01-15
- 1.0.1 — 2015-01-15

## README

# An express-style router middleware for [koa](https://github.com/koajs/koa)

[![Build Status](https://secure.travis-ci.org/TerenceZ/siren-router.png)](http://travis-ci.org/TerenceZ/siren-router)

siren-router extends the [koa-router](https://github.com/alexmingoia/koa-router) by:
* Support for mounting generator function and koa instance using `app.mount`.
* Remove routerPath in Router.
* Auto replacing `ctx.path` and `ctx.params` when enter a router and restore back when exit the router.
* Support middleware array to pass in app[verb].

## Install

```
npm install --save siren-router
```

or

```
npm install git://github.com/TerenceZ/siren-router.git
```

## Usage

The usage is the same as [koa-router](https://github.com/alexmingoia/koa-router), except:

### Multiple routers

You can use multiple routers and sets of routes by omitting the `app`
argument. For example, separate routers for two versions of an API:

```javascript
var koa = require('koa');
  , mount = require('koa-mount')
  , Router = require('koa-router');

var app = koa();

var APIv1 = new Router({ mergeParams: true });
var app2 = koa();

APIv1.get('/sign-in', function *() {
  // ...
});

app2.use(function *() {
  // ...
});

app
  .mount('/v1', APIv1)
  .mount('/v2', app2); // You can mount the application directly.
```

## API

### Router#verb([name, ]path, middleware[, middleware...])

Match URL patterns to callback functions or controller actions using `router.verb()`,
where **verb** is one of the HTTP verbs such as `router.get()` or `router.post()`.

```javascript
app
  .get('/', function *(next) {
    this.body = 'Hello World!';
  })
  .post('/users', [
    function *(next) {
      // ...
    }, function *(next) {
      // ...
    }
  ], function *(next) {
      // ...
  })
  .put('/users/:id', function *(next) {
    // ...
  })
  .delete('/users/:id', function *(next) {
    // ...
  })
  .mount('/users/:id', function *(next) {
    // ...
  });
```

Route paths will be translated to regular expressions used to match requests.

Query strings will not be considered when matching requests.

#### Mounting

Create route for path starting with "/prefix/:id" using `router.mount()` or `router.use()`:

```javascript
app.mount("/prefix/:id", function *(next) {
  // This will match paths like /prefix/abcd, /prefix/abcd/dffgf, etc.
});
```

Notice that the mounting path is forced to be strict, no matter what the `opts.strict` is.

### Auto Replace and Restore the `ctx.path` and `ctx.params`

When enter the router, the `ctx.params` will replace/merge (according to `opts.mergeParams`).

```javascript
app
  .use('/:id', function *(next) {
    console.log(this.path); // => '/update'
    console.log(this.params); // => { id: 'alex' }
    yield *next;
    console.log(this.path); // => '/update'
    console.log(this.params); // => { id: 'alex' }
  })
  .get('/alex/:action', function *(next) {
    console.log(this.path); // => '/'
    console.log(this.params); // => { action: 'update' }
    yield *next;
    console.log(this.path); // => '/'
    console.log(this.params); // => { action: 'update' }
  });
``` 

## Tests

Tests use [mocha](https://github.com/visionmedia/mocha) and can be run
with [npm](https://npmjs.org):

```
npm test
```

## MIT Licensed

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