super-tiny-hash-router

bite-sized front-end router.

MIT License

Downloads
14
Stars
4
Committers
1

super-tiny-hash-router NPM version NPM downloads Build Status

bite-sized (800 bytes) front-end router.

Install

$ npm install --save super-tiny-hash-router

CDN version: https://unpkg.com/super-tiny-hash-router/dist/

Usage

import createRouter from 'super-tiny-hash-router'

const router = createRouter()

router.add('/', path => {
  console.log(path) // eg: '/?foo=bar'
  renderHome()
})

router.add('*', () => {
  render404()
})

// home router and 404 router are required before calling router.init()
router.init()

router.go('/user/egoist')

Note: something like /user/:name is not supported for now.

Middlewares

// the built-in url-parse middleware
import parseUrl from 'super-tiny-hash-router/parse-url'

router.use(parseUrl)

router.add('/', context => {
  console.log(context)
  //=> {params: {}, query: {}}
})

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

License

MIT © EGOIST