eli-router
v0.3.0
Published
Core of a js router
Downloads
19
Readme
eli-router
Core of a js router, supporting nested route with multiple URL paramters and handlers.
Can be integrated into Nodejs or Browser framework such as koa and react.
(For koa)[https://github.com/nameoverflow/eli-router]
Used by (eliter)[https://github.com/nameoverflow/eliter]
USEAGE
let Router = require('eli-router')
let R = new Router()
// :: stands for a URL paramter.
R.route('/admin/::').end(id => `Id: ${id}`)
// URL.pathname = '/admin/1234567'
let matched = R.dispatch(URL.pathname).pop()
Router.handle(matched) // => "Id: 1234567"
R.route('/user/::/').route('/messageto/::').end((user, target) => `User ${user} send message to id ${target}`)
Router.handle(R.dispatch('/user/123/messageto/456').pop()) // => "User 123 send message to id 456"
// work with asterisk
R.route('/something/*').end(() => 'Something')
let matched = R.dispatch('/something/others/blahblah').pop()
Router.handle(matched) // => "Something"
function checkSession() {
// Function to check session...
}
function showID(id) {
// Show id to client....
}
R.route('/user', checkSession).route('/::').end(showID)
R.dispatch('/user/123') // => [[showID, [123]], [checkSession, []]]