A tiny hashchange router inspired by express.js & page.js
hasher is a tiny hashchange router.
Most web applications now use "pushState" for its routing function.
"hashchange" is legacy solution, but it is still useful for the application without server
(for example: local node-webkit applications).
import hasher from 'hasher';
hasher('/', index);
hasher('/user/:id', load, show);
hasher('/user/:id/edit', load, edit);
hasher('/user/:id/delete', del);
hasher('*', notfound);
// Begin monitoring hashchange events
hasher();
- route {String}
- callback {Function}
Set routes and callbacks.
Each callback is invoked params
object and next
function.
function load(params, next) {
params.num; //-> '001'
params.article; //-> 'first-post'
next(); // run `edit` callback
}
function edit() {
console.log('it called after `load` function.');
}
hasher('/blog/:num/:article/edit', load, edit);
hasher.redirect('/blog/001/first-post/edit');
- route {String}
Redirect hashchange route.
Begin monitoring hashchange events.
Stop monitoring hashchange events.
Stop monitoring hashchange events and clear all options.
- page.js by TJ Holowaychuk.