Skip to content

thibauts/router

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 

Repository files navigation

router

Lightweight isomorphic router

Works both server-side in node and in browsers supporting pushState. The module should work with CommonJS and AMD packagers (as long as you provide the eventemitter dependency for the latter).

The package is not published on NPM because I'm too lazy to find a cool (and free) name.

Installation

Server-side :

$ npm install git+https://github.com/thibauts/router.git

Client-side :

$ wget https://raw.githubusercontent.com/thibauts/eventemitter/master/eventemitter.js
$ wget https://raw.githubusercontent.com/thibauts/router/master/router.js

Usage

On the node side :

var Router = require('router');

var router = new Router();

router.define({
  'index': '/',
  'hello': '/hello/:name'
});

router.on('route', function(name, params) {
  console.log('route', name, params);
});

router.on('notfound', function(url) {
  console.log('not found', url);
});

router.dispatch('/foobar');
router.dispatch('/hello/world');

On the browser side (you will need eventemitter) :

<!doctype html>
<html>
<head>
  <script src="eventemitter.js"></script>
  <script src="router.js"></script>
  <script>
    var router = new Router();

    router.define({
      'index': '/',
      'hello': '/hello/:name'
    });

    router.on('route', function(name, params) {
      console.log('route', name, params);
    });

    router.on('notfound', function(url) {
      console.log('not found', url);
    });

    router.navigate('/foobar');
    router.navigate('/hello/world');
  </script>
</head>
<body>
</body>
</html>

By default, router uses pushState, but for compatibility issues you can listen to the hashchange event :

    var router = new Router({useHash: true});

About

Lightweight isomorphic router

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published