Home

Awesome

route.js

npm bower

Very small and simple routing library that can be use on the server or in the browser. It use dependency injection to inject arguments to your function.

It contain only a router so it can be use anywhere. It's only the API so you need to write your own code that will execute when ever you want (like on hashchange or using HTML5 History API).

Install

You can grab the file from repo or from npm:

npm install jroute.js

or from bower

bower install route.js

and inlcude the route.js and you're good to go.

Usage

var router = new route();

// from version 0.4.0 you can use arrow functions

router.match('/foo/bar/{{id}}/{{name}}', function(name, id) {
    // name and id will be in different order
    // names in url need to match names in function
    console.log(name + ' ' + id);
});

router.exec(location.hash.replace(/^#/, ''));

if you want to execute on change of the hash so hyperlinks work you can use this code:

window.addEventListener('hashchange', function() {
    router.exec(location.hash.replace(/^#/, ''));
});

the init exec will also be needed to get init route when you refresh the page

API

router.map('/user/{{name}}', '/user/foo');

it will return {name: "foo"} if pattern don't have variables it will return empty object and if it don't match it will return undefined.

[{
  "pattern": "/user/{{name}}",
  "data": {
    "name": "foo"
  }
}]

for url /user/foo, or empty array if not match found.

License

Licensed under MIT license

Copyright (c) 2014-2017 Jakub T. Jankiewicz