https://github.com/serapath/route-stream
router to stream routes
https://github.com/serapath/route-stream
Last synced: about 1 year ago
JSON representation
router to stream routes
- Host: GitHub
- URL: https://github.com/serapath/route-stream
- Owner: serapath
- License: mit
- Created: 2016-11-22T12:50:19.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-11-22T13:01:40.000Z (over 9 years ago)
- Last Synced: 2024-03-25T03:03:13.123Z (over 2 years ago)
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/route-stream
- Size: 1.95 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# route-stream
router to stream routes
# usage
`npm install route-stream`
```js
var router = require('route-stream')
// prepare some router handlers
function home ({ from, to, data }) {
return `
home:"${from}"=>"${to}",${JSON.stringify(data)}`
}
function products ({ from, to, data }) {
return `products:"${from}"=>"${to}",${JSON.stringify(data)}`
}
function _404 ({ from, to, data }) {
return `404:"${from}"=>"${to}",${JSON.stringify(data)}`
}
// use
var routes = router({ '/': home, '/products': products, 404: _404 })
// OR
var routes = router()
routes(404, _404) // 404 route is optional - router has a default 404 route
routes('/', home)
routes('/products', products)
routes('/products', undefined) // remove route '/products'
console.log(routes instanceof router) // => true
// test
routes.on('data', function parent (route) { console.log(route) })
// NAVIGATE
routes.write({ to: '/' })
routes.write({ to: '/doesntexist', data: { x: 1 } })
routes.write({ to: '/products', data: { x: 1 } })
routes.write({ to: '/' })
routes.write({ to: '/', data: { foo: 'bar' }})
routes.write({ data: { foo: 'only data' }})
```