Lightweight and fast router for JavaScript based on Radix Tree.
undefinedInstall package:undefined
# npm
npm install radix3
# yarn
yarn add radix3
undefinedImport:undefined
// ESM
import { createRouter } from 'radix3'
// CJS
const { createRouter } = require('radix3')
undefinedCreate a router instance and insert routes:undefined
const router = createRouter()
router.insert('/path', { payload: 'this path' })
router.insert('/path/:name', { payload: 'named route' })
router.insert('/path/foo/**', { payload: 'wildcard route' })
*Match route to access matched data:undefined
// { payload: 'this path' }
router.lookup('/test')
// { payload: 'named route', params: { name: 'fooval' } }
router.lookup('/test/fooval')
// { payload: 'wildcard route' }
router.lookup('/path/foo/bar/baz')
// null (no route matched for/)
router.lookup('/')
router.insert(path, data)path can be static or using :placeholders and ** for wildcard paths.
The data object will be returned on matching params. It should be an object like { handler } and not containing reserved keyword params.
router.lookup(path)Returns matched data for path with optional params key if mached route using placeholders.
router.lookupAll(prefix)Find all data nodes matching path prefix.
router.remove(path)Remove route matching path.
See benchmark.
Based on original work of charlieduong94/radix-router
by Charlie Duong (MIT)
MIT - Made with ❤️