1.1.5 • Published 7 years ago

mini-routerjs v1.1.5

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

mini-routerjs

A very basic router with functionalities of parsing route and registering callbacks for browser and nodejs.

Installing

Use via npm:

npm install mini-routerjs
var Router = require('mini-routerjs');

// Use es6 import
import Router from 'mini-routerjs';

Use in browser:

Scripts for browser is under build directory, use router.js for development (contains inline source maps) or use router.min.js for production. The reference in browser is window.MiniRouter.

Examples

Create a common route:

var Router = require('mini-routerjs'),
    router = new Router();

router.create('/foo/:name/bar', function(request){
  console.log(request);
});

router.match('http://hostname/foo/jimmy/bar', 'some data');
/**
* Console output:
*{ url: 
*   { protocol: 'http',
*     auth: '',
*     host: 'hostname',
*     pathname: '/foo/jimmy/bar',
*     query: {},
*     hash: '' },
*  params: { name: 'jimmy' },
*  data: 'some data' }
*/

router.match('/foo/jimmy/bar?foo=bar', 'some data');
/**
* Console output:
*{ url: 
*   { protocol: '',
*     auth: '',
*     host: '',
*     pathname: '/foo/jimmy/bar',
*     query: {foo: 'bar'},
*     hash: '' },
*  params: { name: 'jimmy' },
*  data: 'some data' }
*/

Create a mismatch route:

var router = require('mini-routerjs'),
  router = new Router();
  
router.createMismatch(function(request){
  console.log(request);
});

router.match('/no-match', 'some data');
/**
* Console output:
*{ url: 
*   { protocol: '',
*     auth: '',
*     host: '',
*     pathname: '/no-match',
*     query: {},
*     hash: '' },
*  params: null,
*  data: 'some data' }
*/

Constructor and methods

Router(options)

OptionTypeDescription
strictBooleanEnable strict routing. false by default, '/foo' and '/foo/' are treated the same by the router.
parseQueryBooleanWhether parse the query string to object, default true.

Router.create(route, callback, strict)

ParamsTypeDescription
routeStringThe route pattern.
callbackFunctionThe callback which will be called when the route is matched, default function(){}
strictBooleanThis will override the constructor's strict option.

When a route is matched, the corresponding callback will be passed a request param and executed. The request param has a structure like:

{
  url: url, // A parsed url object which matchs this route.
  params: params, // The params extracted from url.
  data: data // The data passed by match method.
}

Url is parsed by simple-url.

The route support following patterns:

  • * will match any none '/' characters no greedy, the matched part will be indexed as the order in all * and **.
  • ** will match any characters, the matched part will be indexed as the order of in all * and **.
  • :name will match a part of none '/' characters, the matched part will be indexed as 'name'.
  • () will make the part between it optional.
Routeurlparams
/foo/*.gif/foo/hello.gif{'0': 'hello'}
/foo/**/foo/bar/hello{'0': 'bar/hello'}
/foo/**/*.gif/foo/bar/hello.gif{'0': 'bar', '1': 'hello'}
/foo/:bar/hello/foo/bar/hello{'bar': 'bar'}
/foo/:bar/*.gif/foo/bar/hello.gif{'bar': 'bar', '0': 'hello'}
/foo/(:bar/hello)/foo/bar/hello{'bar': 'bar'}
/foo/(:bar/hello)/foo/{'bar': undefined}

Router.match(url, data)

Do a match with url and pass data to callback.

Router.createMissmatch(callback);

Create a missmatch, the callback will be called with a request param when any match is failed.

License

MIT

1.1.5

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

8 years ago

1.1.0

8 years ago

1.0.1

8 years ago

1.0.0

8 years ago