Router

How to install?

npm i --save vqua-router

Example

const { route, matchRoutes, initRoutes } = require('vqua-router') class PostsController { index() {} show() {} create() {} } const routes = [ route('/', 'Posts#index', {}, [ route('/posts/:id', 'Posts#show'), route('/posts/create', 'Posts#create'), ]), ] const controllers = { PostsController: new PostsController } const initedRoutes = initRoutes({ routes, controllers }) // console.log(initedRoutes) // // [ { path: '/', // action: [Function: index], // segments: [ '' ], // props: {}, // index: 0, // controller: PostsController {} }, // { path: '/posts/:id', // action: [Function: show], // segments: [ 'posts', ':id' ], // props: {}, // index: 1, // controller: PostsController {} }, // { path: '/posts/create', // action: [Function: create], // segments: [ 'posts', 'create' ], // props: {}, // index: 2, // controller: PostsController {} } ] const matchedRoute = matchRoutes(initedRoutes, '/posts/2') // console.log(matchedRoute) // // { path: '/posts/:id', // action: [Function: show], // segments: [ 'posts', ':id' ], // props: {}, // index: 1, // controller: PostsController {}, // request: // { path: '/posts/2', // segments: [ 'posts', '2' ], // params: { id: '2' } } }

Description

A set of utilities for organizing routing.


route [Function]

Creates a route object.


Arguments:

matcher|matchers matcher [Object] | [Array]: matcher [Object] - matcher object(s)
action [Function] - function handler or string in the format 'Controller # action'
For example, for the PostsController controller and the show method, the path string will be 'Posts#show'
props [Object] - parameter object
childs [Array] - array of nested routes


Return:


route [Object]


scope [Function]

Assigns to all children of the first level their segments.


Arguments:

matcher|matchers matcher [Object] | [Array]: matcher [Object] - matcher object(s)
childs [Array] - array of nested routes


Return:

[Array]: route [Object]


initRoutes [Function]


Arguments:


routes [Array] - array of routes
controller [Object] - controllers object. Where the keys are the name of the controllers.
matchers [Object] - matchers object. Where the keys are the name of the matchers.


Return:


[Array]: route[Object]


matchRoutes [Function]

Finds the first available route based on the specified path and adds to it is the request object.


Arguments:


routes [Array] - array of routes
path [String] - path string


Return:


[Object]: route[Object] & request[Object]




route [Object]

Route object.


Keys:

segments [Array] - array of path string segments
path [String] - path string
action [Function] - request handler function
props [Object] - route parameters object
childs [Array] - array of nested routes


request [Object]

Request object.


Keys:


path [String] - path string
segments [Array] - array of path elements
params [Object] - route parameter object


matcher [Object]

Match object.


Keys:

key [String] - matching segment name
match [Function] - function callback


Example:

{ key: 'id', matcher: id => id == '5' }


matcher.match [Function]

Determines whether the value of the url segment is valid.


Arguments:

value [String] - value of the url segment


Return:


[Boolean]


matcher [String]

Match string


Examples: