pull-http-router
v2.0.0
Published
Minimal express-like router for pull-streams
Downloads
2
Readme
pull-http-router
Minimal express-like router for pull-streams.
Usage
const pl = require('pull-level')
const summary = require('server-summary')
const toPull = require('stream-to-pull-stream')
const http = require('http')
var router = new PullHttpRouter()
router.post('/api/messages', pl.write(db), setStatus(200))
router.get('/api/messages', [
pull.map(function readDB (request) {
return pull(
pl.read(db, {min: request.query.offset}),
offsetLimit(null, request.query.limit)
)
}),
pull.flatten(),
setHeaders,
setStatus(200)
])
var server = http.createServer(function (req, res) {
pull(
parseHeaders,
parseQueryStrings,
router.route(),
toPull(res)
)
})
server.listen(1337, summary(server))
API
PullHttpRouter
Usage: router = new PullHttpRouter(opts)
Create a new PullHttpRouter.
Accepts an options object with the following optional keys:
- router: Accepts any router object with a match and define method. Defaults to routington
- match: Specify a specific match method for matching routes. Defaults to routington#match
- define: Specify a specific define method for defining routes. Defaults to routington#define
router#route
Usage: router.route(opts)
Create a pull-stream which expects a request object, and returns a readable stream.
pullHttpRouter#get, pullHttpRouter#set, pullHttpRouter#update, pullHttpRouter#post, pullHttpRouter#put, pullHttpRouter#delete, pullHttpRouter#options
Usage: router.get(path, [streams...])
Accepts a path to match on, and a stream to stream to. If multiple pull-streams are provided they will be linked together as if they were passed to pull-stream core's pull()
Installation
$ npm install pull-http-router