Intro

Router for byte-conscious isomorphic applications designed for use with Browserify.

After using several different routers I decided none of them were quite what I wanted. So, I wrote my own.
This router is loosly inspired by the Express router, react-router, and page.js.

Tested on Node.js 0.12.

Features

kingfish has the following featureset:

Planned features:

Start

Install with npm:

npm install --save kingfish

In Node.js or Browserify, load it with:

var Kingfish = require('kingfish');

Using kingfish in the browser is just two lines, plus your routes.

var router = new Kingfish(routes, document.getElementById('content'));
router.start();

On the server, you can handle a request with:

var router = new Kingfish(routes);
var content = router.renderString(url);

Creating a sub-router in the browser is similar:

var childRouter = new Kingfish(childRoutes, document.getElementById('childContent'));
childRouter.render(subUrl);

Finally, creating a sub-router on the server is identical to a regular server router.

Routes are just objects, in ES6 these are very easy to create with the class keyword. A basic route with both a server and browser render method might look like:

var routes = {
	'/post/?': class {
		renderString() {
			return 'This is the post with ID ' + this.params[0];
		}
		mount() {
			this.element.innerHTML = this.renderString();
		}
	}
}

Inside your route handlers you can use any libraries you choose, including Handlebars, React, Backbone, or even another router.

Docs

API

When you require('kingfish') you will get back a constructor for the Kingfish object. Create the object with the following two parameters:

On the server, the resulting object instance will have just one method:

On the browser, it will have three:

An important part of modular applications is the ability to define sub-routes. With kingfish, this is done by creating a sub-router.

On the server, there is no difference between a regular router and a sub-router.

In the browser, create the sub-router the same way, but use the render method instead of calling start. You will need to call it every time the URL changes.

See the demo application for an example of how to use all the above methods, located in the “demo” folder of the GitHub repository.

Routes definition

The routes definition is an object with keys equal to the route patterns to match. These patterns support two special character sequences for extracting parameters:

The values in the routes definition are objects (I recommend using ES6 classes for readability) with the following properties and methods:

For server-side rendering, renderString is the only method you need. For client-side rendering, you need either renderString or mount; if both are present, mount will be used. If you have URL parameters you should implement setParams as well, and you will get a warning if it does not exist when the parameters change.

Contributing

Bug requests and pull requests are welcome. Keep the following in mind:

To develop, checkout the repository from GitHub and install the dependencies:

Run build scripts with npm:

Set NODE_ENV to the following to affect the build process. This affects the build of the module, demo, and docs.

License

   Copyright 2015 Paul Rayes

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.