Skip to content
/ hasher Public

Tiny hashchange router inspired by express.js & page.js.

Notifications You must be signed in to change notification settings

narirou/hasher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

43 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

hasher

A tiny hashchange router inspired by express.js & page.js

Build Status Test Coverage MIT Licensed

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).

Usage

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();

API

hasher( route, callback[, callback...] ) / hasher.set(...)

  • 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');

hasher( route ) / hahser.redirect(...)

  • route {String}

Redirect hashchange route.

hasher() / hasher.start()

Begin monitoring hashchange events.

hasher.stop()

Stop monitoring hashchange events.

hasher.reset()

Stop monitoring hashchange events and clear all options.

Based on

About

Tiny hashchange router inspired by express.js & page.js.

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published