๐ข๐ Shipharbor is a tiny router, that runs in browsers and in nodejs.
$ npm install shipharbor
const Shipharbor = require('shipharbor');
const router = new Shipharbor();
router.add('/', () => {
console.log('> Home');
});
router.add('/user/:id', (params) => {
console.log('> User: ' + params.id);
});
router.add('/*', () => {
console.log('> Not found');
});
router.match('/').handler();
// > Home
const match = router.match('/user/my-user');
match.handler(match.params);
// > User: my-user
router.match('/whatever').handler();
// > Not found
Type: string
A string representing the path you want to match. This is similar to express's paths.
The supported pattern types are:
- static (
/users
) - named parameters (
/users/:id
) - nested parameters (
/users/:id/books/:title
) - optional parameters (
/users/:id?/books/:title?
) - any match / wildcards (
/users/*
)
Type: function
The handler function for a specific path.
Returns: object|boolean
;
Type: string
The url you want to match against the previously specified routes.
const match = router.match('/route');
// if the route exists:
// => { handler, params }
// else
// => false
match.handler(); // executes the handler
MIT ยฉ Tobias Herber