Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tivac/router
Writing a JS router, because I can.
https://github.com/tivac/router
javascript routing single-page-applications
Last synced: 3 months ago
JSON representation
Writing a JS router, because I can.
- Host: GitHub
- URL: https://github.com/tivac/router
- Owner: tivac
- License: mit
- Created: 2018-08-05T07:19:42.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2018-10-19T05:20:58.000Z (over 6 years ago)
- Last Synced: 2024-10-12T00:52:13.407Z (4 months ago)
- Topics: javascript, routing, single-page-applications
- Language: JavaScript
- Homepage:
- Size: 269 KB
- Stars: 4
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @tivac/router
A very-simple router, inspired by [`page.js`](https://www.npmjs.com/package/page).
(You probably want to use `page.js`, honestly. This is just me experimenting!)
## Usage
Include in a `` tag
```html
<script src="https://unpkg.com/router/dist/router.umd.js>
```Or with `import`
```js
import router from "router";
```Or with `require`
```js
const router = require("router");
```## API
### `router(path, callback[, callback ...])`
Create a route mapping `path` to `callback`. See [Route Callbacks](#route-callbacks) for a description of the signature.
Returns the same `router` instance for optional chaining.
`path` supports `/:params` as well as `*` or `/path/*` style wildcards. No regex stuff though.
### `router.go(path, fn)`
Begin route matching for a path. `fn` will be called when all matched route handlers are complete.
### `router.unknown(fn)`
Define a `fn` that will be called when a route handler cannot be found.
## Route Callbacks
Route callbacks are passed two arguments:
1. `ctx`, a shared object available to all handlers of a request
2. `next()`, a function that will invoke any further route handlers.