a-route
Social Media Photo by Jakub Gorajek on Unsplash
Express like routing, as Custom Element or standalone, inspired by page.js.
app API
app.get(path:string|RegExp, cb:Function[, cb2, ...]):app
to subscribe one or more callbacks for the specified routeapp.delete(path:string|RegExp, cb:Function[, cb2, ...]):app
to unsubscribe one or more callbacks for the specified routeapp.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 routeapp.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.
<script src="//unpkg.com/a-route"></script>
<a is="a-route" href="/test/?query=value">test query</a>
<a is="a-route" href="/test/OK" no-propagation replace>test OK</a>
<a is="a-route" href="/whatever">test 404</a>
const {app} = ARoute;
app
.get('/test/?query=:query', function (ctx) {
console.log(ctx);
})
.get('/test/:status', function (ctx) {
console.log(ctx);
});
app.get('*',
function (ctx, next) {
console.log(ctx);
next();
},
console.error
);