ks-route-parser
v0.0.1
Published
A Nodejs YML route parser
Downloads
103
Readme
Route parser
It a simple route parser accepting a list of YML files and return a list of resolved routes like :
home:
pattern: '/home'
post:
controller: 'ControllerMock:add'
middlewares: [
controller: 'Auth:isAuth'
]
And creates a new instance of :
/**
* The route definition class
*/
export default class Route {
/**
* Create a new route
* @param {string} pattern The pattern URL
* @param {string} method The HTTP method
* @param {Object} controllerInstance The controller instance object
* @param {string} controllerMethod The controller method
* @param {Object[]} middlewares = [] The middleware list to start before the route is resolved
*/
constructor (pattern, method, controller, controllerMethod, middlewares = []) {
/**
* The pattern URL
* @type {string}
*/
this.pattern = pattern
/**
* The HTTP method
* @type {string}
*/
this.method = method
/**
* The controller object
* @type {Object}
*/
this.controller = controller
/**
* The controller method
* @type {string}
*/
this.controllerMethod = controllerMethod
/**
* The middleware list to start before the route is resolved
* @type {Object[]}
*/
this.middlewares = middlewares
}
}
Usage
You can simply call this :
import RouteParser from 'route-parser'
/**
* Create a new route parser
*/
const routeParser = new RouteParser()
/**
* Get a list of YML files to parse
* Returns the route parsed with the above model
*/
const routes = routeParser.parseRoutes(['path/to/file/routes.yml'])