timwis / nanorouter

Smol frontend router

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

nanorouter stability

npm version build status downloads js-standard-style

Smol frontend router

Usage

var nanorouter = require('nanorouter')
var router = nanorouter({ default: '/404' })

router.on('/foo', function (params) {
  console.log('hit /foo')
})
router.on('/foo/:bar', function (params) {
  console.log('hit a route with params', params.bar)
})
router.on('/foo#baz', function (params) {
  console.log('we do hash routes too!')
})
router.on('/foo/*', function (params) {
  console.log('and even wildcards', params.wildcard)
})

router.emit('/foo/hello-planet')

FAQ

How is this different from sheet-router?

sheet-router does slightly more and has a different syntax. This router is lighter, faster and covers less concerns. They're pretty similar under the hood though.

API

router = nanorouter([opts])

Create a new router. opts can be:

  • opts.default: set a default handler in case no route matches. Defaults to /404
  • opts.curry: return the handler function rather than calling it directly. Useful when the same path can be called many times in a row.

router.on(routename, handler(params))

Register a handler on a routename. The handler receives an object with params on each render. A result can be returned the caller function.

result = router(routename)

Call a handler for a routename. If no handler matches, the handler specified in opts.default will be called. If no default handler matches, an error will be thrown. Results return from the called handler will be returned from this function.

See Also

License

MIT

About

Smol frontend router

License:MIT License


Languages

Language:JavaScript 100.0%