Giter Site home page Giter Site logo

nanorouter's Introduction

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

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.emit(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 returned from the called handler will be returned from this function.

matchedRoute = router.match(route)

Matches a route and returns an object. The returned object contains the properties {cb, params, route}. This method does not invoke the callback of a route. If no route matches, the route specified in opts.default will be returned. If no default route matches, an error will be thrown.

Note that router() does not affect browser history. If you would like to add or modify history entries when you change routes, you should use history.pushState() and history.replaceState() alongside router().

See Also

License

MIT

nanorouter's People

Contributors

bendik avatar davidtheclark avatar goto-bus-stop avatar marcbachmann avatar marlun avatar timwis avatar yoshuawuyts avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.