esrol-router
You can create new routes, set allowed ways of reaching them, handle request, get information regarding those routes, and set middleware.
Part of Esrol
Installation
$ npm install --save esrol-router
Usage
'use strict';const Router = ;const http = ;const router = ;const route = url: '/posts' { return res; } { return res; };router;router; http; //curl localhost:3333/posts//curl localhost:3333/posts/1
Please see the docs here, for information how to structure your route
Methods
- registerRoute(route) ⇒
boolean
Register a route.
- getRoutesLength() ⇒
int
Get the ammount of registered routes.
- getRouteMethodsLength(url) ⇒
int
Get the methods number in the route.
- setSupportedHttpMethods(methods) ⇒
boolean
Set the supported http methods.
- setNamespace(namespace) ⇒
boolean
Set server namespace.
- setMiddleware(middleware) ⇒
boolean
Initialise middlewares.
- onRequest(req, res) ⇒
object
Handle a request and route it to the required route.
boolean
registerRoute(route) ⇒ Register a route.
Returns: boolean
- true - returns true if registering was successful
Throws:
error
- throws error if thrown by registerRoute in Routes
Param | Type | Description |
---|---|---|
route | object |
a route object, containing intormation for the route |
int
getRoutesLength() ⇒ Get the ammount of registered routes.
Returns: int
- - ammount of registered routes
int
getRouteMethodsLength(url) ⇒ Get the methods number in the route.
Returns: int
- 0 or integer - returns 0 if there are no set routes,
or an integer number
Param | Type | Description |
---|---|---|
url | string |
path to the route |
boolean
setSupportedHttpMethods(methods) ⇒ Set the supported http methods.
Returns: boolean
- true - returns true on success
Throws:
error
error - if thrown by setSupportedHttpMethods
Param | Type | Description |
---|---|---|
methods | array |
allowed http methods |
boolean
setNamespace(namespace) ⇒ Set server namespace eg 'v1' which will evaluate www.example.com/v1
Returns: boolean
- true
Param | Type | Description |
---|---|---|
namespace | string |
eg /v1 |
boolean
setMiddleware(middleware) ⇒ Initialise middlewares.
Returns: boolean
- true
Throws:
error
error - if thrown by setMiddleware
Param | Type | Description |
---|---|---|
middleware | function |
has to have 4 parameters |
object
onRequest(req, res) ⇒ Handle a request and route it to the required router.
Returns: mixed
- mixed - returns the value from the route // if returned
Param | Type | Description |
---|---|---|
req | object |
request |
res | object |
response |
Contriubtion
Any contribution will be highly appreciated. Just make sure that:
- Your code works.
- You have 100% successful tests coverage.
- You have comments in your code.
- Follows eslint config. Exceptions are possible where that make sense.
Tests
To run the test suite, first install the dependencies, then run npm test
:
$ npm install$ npm test