@plant/router
v1.2.0
Published
Router for plant server
Downloads
34
Readme
Plant/Router
Router for Plant server.
Table of Contents
Install
npm i @plant/router
Usage
Strict route example:
const Router = require('@plant/router')
const router = new Router()
router.get('/users/:id', ({res, route}) => {
// Get params
route.params.id // -> {id: "1"}
// Get current path segment
route.path // '/users/1'
// Path before router
route.basePath // '/'
})
Wildcard route with nested router example:
const Router = require('@plant/router')
const router = new Router()
router.use('/posts/*', Router.create((router) => {
router.get('/:id', async () => {
// ...
})
router.post('/', async () => {
// ...
})
}))
API
Router Type
Router allows to filtrate request by HTTP method and URL pattern and to extract values from the URL.
Example
const Router = require('@plant/router')
const router = new Router()
router.get('/users/:id', () => { /* get resource */ })
router.post('/users', () => { /* post resource */ })
router.delete('/users/:id', () => { /* delete resource */ })
Router.Router()
() -> Router
Router constructor has no arguments.
Router.create()
(create: (router: Router) -> void) -> Router
Factory method. Accepts create
method as argument which is a function
with router configuration logic. Example:
Router.create((router) => {
router.get('/greet', ({res}) => res.text('Hello World'))
})
Router#use()
(route:String, ...handlers:Handle) -> Router
Method to add handler for any HTTP method.
Router#before()
(...handlers:Handle) -> Router
Add handlers which will be fired before each handler defined by .use
, .get
,
and other route handlers when route params are match route params
(route and method).
It can be helpful when you need to prepend each route with expensive computations which should be done only if request matches defined options. It could be user loading or disk reading.
Router#get()
(route:String, ...handlers:Handle) -> Router
Specify route
handlers
for GET
HTTP method.
Example
// Strict route
router.get('/users/:id', () => {})
// Wildcard route
router.get('/posts/*', () => {})
Router#post()
Same as Router#get() but for POST
HTTP method.
Router#put()
Same as Router#get() but for PUT
HTTP method.
Router#patch()
Same as Router#get() but for PATCH
HTTP method.
Router#delete()
Same as Router#get() but for DELETE
HTTP method.
Router#head()
Same as Router#get() but for HEAD
HTTP method.
Router#options()
Same as Router#get() but for OPTIONS
HTTP method.
Router#addRoute()
(method:string|string[], route:string, ...handler:Handle) -> Router
Add route handler and return router instance. Use method
to specify HTTP
method or methods supported by the route
handler
.
License
MIT © Rumkin