An open API service indexing awesome lists of open source software.

https://github.com/webreflection/a-route

Express like routing as Custom Element or standalone
https://github.com/webreflection/a-route

Last synced: 10 months ago
JSON representation

Express like routing as Custom Element or standalone

Awesome Lists containing this project

README

          

# a-route

**Social Media Photo by [Jakub Gorajek](https://unsplash.com/@cinegeek) on [Unsplash](https://unsplash.com/)**

Express like routing, as Custom Element or standalone, inspired by [page.js](https://visionmedia.github.io/page.js/).

### app API

* `app.get(path:string|RegExp, cb:Function[, cb2, ...]):app` to subscribe one or more callbacks for the specified route
* `app.delete(path:string|RegExp, cb:Function[, cb2, ...]):app` to unsubscribe one or more callbacks for the specified route
* `app.navigate(path:string[, operation:string = 'push']):void` to navigate to the first matching route for the given path. By default, it pushes to the history but it could `replace`, if the second parameter is the _replace_ string, or `ignore`.
* `app.param(path:string|RegExp):app` to subscribe to a specific parameter regardless of the route
* `app.use(path:string|RegExp):app` to subscribe a callback for a specific mount point or all of them

### Example

The following is a basic example, also [available live](https://webreflection.github.io/a-route/test/?).

```html

test query

test OK

test 404
```

```js
// import {app} from 'a-route';
// const {app} = require('a-route');
const {app} = ARoute;

// define routes
app
.get('/test/?query=:query', function (ctx) {
console.log(ctx);
/*
{
"path": "/test/?query=value",
"params": {
"query": "value"
}
}
*/
})
.get('/test/:status', function (ctx) {
console.log(ctx);
/*
{
"path": "/test/OK",
"params": {
"status": "OK"
}
}
*/
});

// intercept all unregistered calls
app.get('*',
function (ctx, next) {
console.log(ctx);
/*
{
"path": "/whatever"
}
*/
next();
},
// will receive the ctx object too
console.error
);
```