# generic-router

> Generic Router

Latest version **0.1.0** (published 2020-10-11) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.1.0 |
| Published | 2020-10-11 |
| First published | 2020-10-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 7.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Thomas Bergwinkl |
| Maintainers | bergos |

## Links

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

## Dependencies (1)

- [path-to-regexp](https://npm.io/package/path-to-regexp.md) ^6.2.0

## Recent versions

- 0.1.0 (latest) — 2020-10-11

## README

# generic-router

A very basic generic router.

## Usage

The package exports a `Router` class that can be imported like this:

```javascript
import Router from 'generic-router'
```

### Router()

Creates a new `Router` instance.
It handles routes defined by a `path` string that can contain parameter variables.
Variables must be prefixed with a colon like this: `:variable`.
The callback function of a route is processed async.
If a callback returns `false`, further matching routes will be processed.

A callback will be called like:

```javascript
await callback(path, params, context)
```

Where `path` and `context` are forwarded from the arguments given to the `handle()` method.
`params` is an object with key-value pairs of the parameters extracted from the `path`.  

### path(path, callback)

Adds a route with the given `callback` for the resource matching the `path` and deeper structures.

### resource(path, callback)  

Adds a route with the given `callback` for the resource matching exactly the `path`.

### async handle(path, context)

Calls callbacks for routes matching the `path` as defined by the `path()` and `resource()` method.
The callbacks will be called in the order they were added to the router.
The return value of the last callback will be forwarded.

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