Fast and simple http routing using radix tree
npm install @appolo/route --save
import {Router} from "@appolo/route"
let router = new Router()
.get("/test/:param", ()=>"working");
output = router.find("GET" , "/test/test2");
console.log(output.params.param) // test2
console.log(output.handler()) // working
Options
useCache
- cache the find result by path, default :true
maxCacheSize
- The maximum size of the cache routes, default:1000
decodeUrlParams
- true to decodeURIComponent params value, default:false
let router = new Router()
.get("/test/test2", ()=>"working");
.post("/test/test2", ()=>"working post");
let router = new Router()
.get("/test/:userId/:userName", ()=>"working");
let router = new Router()
.get("/*", ()=>"working");
same syntax as path-to-regexp
let router = new Router()
.get("/test/:file(^\\d+).png", ()=>"working");
find route by path return object with params
and handler
if no params
found return empty object
import {Router} from "@appolo/route"
let router = new Router()
.get("/users/:userId", (userId)=>console.log(userId));
result = router.find("GET" , "/users/123");
result.handler(result.params.userId) // 123
register new Route handler using Http.Get Method
register new Route handler using Http.Post Method
register new Route handler using Http.Put Method
register new Route handler using Http.Put Method
register new Route handler using Http.Patch Method
register new Route handler using Http.Delete Method
register new Route handler using Http.Delete Method
register new Route handler. multi methods supported
let router = new Router()
.add(["POST","GET"],"/test/:param", ()=>"working");
remove Route by path. multi methods supported
let router = new Router()
.add(["POST","GET"],"/test/:param", ()=>"working")
.remove("Get","/test/:param");
reset the router remove all routes
MIT