Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/bergos/generic-router
Generic Router
https://github.com/bergos/generic-router
Last synced: 2 days ago
JSON representation
Generic Router
- Host: GitHub
- URL: https://github.com/bergos/generic-router
- Owner: bergos
- Created: 2020-10-11T22:36:47.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-10-11T22:38:20.000Z (over 4 years ago)
- Last Synced: 2024-12-06T05:16:43.008Z (about 2 months ago)
- Language: JavaScript
- Size: 1.95 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
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.