As light as a leaf, leafeon is a Javascript routing library that fits perfectly with client-side templating.
Table of Content
- Features
- Overview
- Usage
- Browser support
- API
.add(name: string, path: string, callback: function)
.map(prefixName: string, prefixPath: string, routes: Array)
.fetchRoute(name: string[, parameters: object])
.route: object
.setErrorCallback(callback: function)
.notFoundException()
.before(path: string, callback: function)
.run([callback: function])
- License
Features
- Dynamic routing & URL generator
- Error handling
- Before and after router middleware
- Route mapping
- Browser & npm usage
Overview
A simple route
leafeon;
A simple route using parameter
leafeon;
Register a callback when route is not found. It returns router object.
leafeon;
Mapping routes using a route prefix
// This will create two routes under /docs prefixleafeon;
Usage
npm
Install the package :
npm i leafeon --save
const leafeon = ;// or using ES6// import * as leafeon from 'leafeon';// const router = leafeon.Router(); leafeon leafeon;
Browser
- Include leafeon.js in or at the end of the
<!-- or via jsdelivr CDN -->
- Init the router
const leafeon = ;
- Create some routes and run the router
leafeon ;
Browser support
Supports IE 11+, Chrome 43+, Opera 29+, and Firefox 41+
API
.add(name: string, path: string, callback: function)
Register a route. Use :
in path to create a parameter. It returns the router object.
.map(prefixName: string, prefixPath: string, routes: Array)
Register several routes using a prefix name and path. Routes must be an array of object that follows this schema :
{
name: string,
path: string,
callback: function
}
.fetchRoute(name: string[, parameters: object])
Fetch a registered route by name or path. For dynamic routes, It'll generate the path using given parameters.
leafeon; // or .fetchRoute('/'); // with parametersleafeon;
.route: object
Get the current route :
{
name: string,
path: string,
callback: function,
paramsEnabled: boolean,
params: array
}
.setErrorCallback(callback: function)
Set the not found exception. It returns the router object.
Example :
// overwrite the default not found exceptionleafeon;
.notFoundException()
Call the not found exception callback.
.before(path: string, callback: function)
Register a middleware that will be executed before given path. Type *
to target every routes. It returns the router object.
.run([callback: function])
Run the router with registered routes. Optionally, register a middleware that will be executed after every routes callback.
License
This repository is MIT licensed.
Icon made by Good Ware from Flaticon under CC 3.0 BY license.