Skip to content

Latest commit

 

History

History
62 lines (46 loc) · 1.53 KB

README.md

File metadata and controls

62 lines (46 loc) · 1.53 KB

routes

Route matcher devised for shared rendering JavaScript applications

featuring

  • Zero production dependencies

install

npm install -S @nichoth/routes

ESM vs CJS

Featuring ESM & CJS versions via package.json exports field.

// esm
import Router from '@nichoth/routes'
// cjs
const Router = require('@nichoth/routes').default

example

Get a router instance

const Router = require('@nichoth/routes').default
var router = Router()

Add some routes

router.addRoute('/articles', getArticles);
router.addRoute('/articles/:slug', getArticleBySlug);
router.addRoute('/articles/search/*', searchForArticles);

Find a match

router.match('/articles');

You'll get null back if no route matches the provided URL. Otherwise, the route match will provide all the useful information you need inside an object.

Key Description
action The action passed to addRoute as a second argument. Using a function is recommended
next Fall through to the next route, or null if no other routes match
route The route passed to addRoute as the first argument
params An object containing the values for named parameters in the route
splats An object filled with the values for wildcard parameters

License

MIT

(originally derived from ruta3)