swork-router
swork-router is router middleware for swork powered by path-to-regexp. It is built with TypeScript and async methods.
License
MIT
Installation
npm install swork-router
yarn add swork-router
Example
;; ;; router.get"/hello/:id",; app.userouter.routes; app.listen;
Methods
get | post | put | patch | delete | head | options | all
Create a route using the HTTP verb as your method name such as router.get(...)
or router.post(...)
. In addition, all
is available to match on all HTTP verbs.
router.get"/foos", ;
You are able to pass in a single path or an array of paths with the middleware to be invoked when a path is matched.
router.get,;
Every verb method returns the router instance allowing chaining of path handlers.
router .get"/foos", .post"/foos", .all"/foos/:id",;
Route paths must start with a slash and end without one. Paths are translated to regular expressions using path-to-regexp. As a result, query strings are not evaluated when matching requests.
use
use
allows the nesting of routers. This is useful when building a routes in a separate module.
; router.usegetFooApiRouter;router.usegetBarApiRouter; app.userouter.routes;
Configuration
Prefix
Routes can be prefixed at the router level.
; router.get"/", ...; // responds to "/foos"router.put"/:id", ...; // responds to "/foos/:id"
Origin
Alternate origins can be used but default to configuration.origin
from swork.
; router.get"/world",;
Url Parameters
Named route parameters are captured and added to context.params
property.
router.put"/:id/:name",;
Contact
If you are using swork or any of its related middlewares, please let me know on gitter. I am always looking for feedback or additional middleware ideas.