@iljucha/route
v1.0.10
Published
simple route object
Downloads
2
Readme
Route
Simple Route Object.
Usage
import Route from "@iljucha/route"
let route = new Route("GET", "/", () => console.log("welcome home"))
const ROUTES = []
ROUTES.push(new Route("GET", "/home", () => console.log("welcome home again")))
ROUTES.push(route)
// parameters are also possible, gets them via: paramRoute.params(request.url)
let paramRoute = new Route("GET", "/user/:alias"), () => console.log("user alias")
ROUTES.push(paramRoute)
// can be used in middlewares or http servers
let requestURI = "/user/iljucha"
let requstMethod = "GET"
let responseRoute = ROUTES.find($ => $.match(requestMethod, requestURI))
if (responseRoute) {
let params = responseRoute.params(requestURI)
console.log(params) // => { alias: "iljucha" }
responseRoute.handler() // do your thing
}
else {
// route not found...
}