Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/thibauts/router
Lightweight isomorphic router
https://github.com/thibauts/router
Last synced: 21 days ago
JSON representation
Lightweight isomorphic router
- Host: GitHub
- URL: https://github.com/thibauts/router
- Owner: thibauts
- Created: 2014-06-25T15:16:24.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-11-10T22:34:09.000Z (about 10 years ago)
- Last Synced: 2024-10-16T07:21:28.302Z (2 months ago)
- Language: JavaScript
- Size: 178 KB
- Stars: 1
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
router
======
### Lightweight isomorphic routerWorks 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 :
```bash
$ npm install git+https://github.com/thibauts/router.git
```Client-side :
```bash
$ 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 :
``` javascript
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](https://github.com/thibauts/eventemitter)) :
``` html
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');
```
By default, router uses pushState, but for compatibility issues you can listen to the hashchange event :
``` javascript
var router = new Router({useHash: true});
```