@exobase/use-router
v1.0.0-rc.20
Published
Exobase routing hook
Downloads
173
Readme
title: 'useRouter' description: 'A hook to route requests to Exobase handlers' group: 'Hooks'
Provides an Exobase hook that does method + url routing using a trie based on the given url path.
Install
yarn add @exobase/use-router
# or
yarn add @exobase/hooks
Import
import { useRouter } from '@exobase/use-router'
// or
import { useRouter } from '@exobase/hooks'
Usage
import https from 'https'
import { error } from '@exobase/core'
import { compose, toInt } from 'radash'
import { useRouter } from '@exobase/hooks'
import { useNode } from '@exobase/use-node'
const server = https.createServer(
compose(
useNode(),
useRouter(router =>
router
.put('/v1/library/book/{id}/return', returnBook)
.post('/v1/library/book', createBook)
.get('/v1/library/book/{id}', findBook)
.get('/v1/library/book', listBooks)
),
async () => {
throw error({
status: 404,
message: 'Not found'
})
}
)
)
const port = toInt(process.env.PORT, 8500)
server.listen(port, () => {
console.log(`API listening on port ${port}`)
})