brick-router
Version:
Modular router for serving static assets
95 lines (74 loc) • 3.45 kB
Markdown
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][coveralls-image]][coveralls-url]
[![Downloads][downloads-image]][downloads-url]
[![js-standard-style][standard-image]][standard-url]
Modular router for serving static assets.
```bash
$ npm install brick-router
```
```js
const createPost = require('@myApp/createPost')
const brick = require('brick-router')
const posts = require('./posts.json')
const router = brick()
module.exports = router
router.on('/', cb => cb(null, '/'))
router.on('/index.html', cb => cb(null, 'html'))
router.on('/index.css', cb => cb(null, 'css'))
// register all blogposts
posts.forEach(post => router.on(post, createPost(post)))
router.match('/index.html', function (err, data) {
console.log(data)
// => html
})
// execute all routes and
// write output to directory tree
router.build(__dirname + '/build')
```
There should be no disparity in file serving during development and production.
Current tools either require ecosystem buy-in (grunt, gulp) or focus on a
specific part of the process (wzrd for development, metalsmith for production).
`brick-router` can execute arbitrary code for each page during development
(e.g. watch files and rebuild on changes). When moving to production call
`router.build('/mydir')` to execute all routes and write the output to a
directory tree so it can be served statically.
`'tools.production' === 'tools.development'`
The api is heavily copied from [wayfarer](https://github.com/yoshuawuyts/wayfarer) to maintain a sense of familiarity
between non-method routers.
Create a new router.
Register a new path in the router.
Match a path on the router, pass in an optional callback to the router which
can later be called.
Execute all routes and write the output to a directory tree so it can be served
statically. Calls an optional callback on completion.
This has not been tested yet, but reloads could be called through event
listeners or similar. The livereload server can just live in a separate
function.
- [wayfarer](https://github.com/yoshuawuyts/wayfarer) - client-side router
- [watchify](https://github.com/substack/watchify) - watch mode for browserify builds
- [chokidar](https://github.com/paulmillr/chokidar) - A neat wrapper around node.js fs.watch / fs.watchFile
- [wzrd](https://github.com/maxogden/wzrd) - Super minimal browserify development server
[](https://tldrlegal.com/license/mit-license)
[]: https://img.shields.io/npm/v/brick-router.svg?style=flat-square
[]: https://npmjs.org/package/brick-router
[]: https://img.shields.io/travis/yoshuawuyts/brick-router.svg?style=flat-square
[]: https://travis-ci.org/yoshuawuyts/brick-router
[]: https://img.shields.io/coveralls/yoshuawuyts/brick-router.svg?style=flat-square
[]: https://coveralls.io/r/yoshuawuyts/brick-router?branch=master
[]: http://img.shields.io/npm/dm/brick-router.svg?style=flat-square
[]: https://npmjs.org/package/brick-router
[]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
[]: https://github.com/feross/standard