Unleash awesomeness. Private packages, team management tools, and powerful integrations. Get started with npm Orgs »

route-builder

1.2.0 • Public • Published

route-builder

route-builder is a simple path matcher and path maker based on path-to-regexp (same as Express). It is meant to serve as the foundation for a router.

route-builder borrows on the work of routr and reverend.

Usage

Adding routes

  var RouteBuilder = require('route-builder');
 
  // via the constructor
  var router = new RouteBuilder([
    ['home', '/'],
    ['post', '/post/:id', { a: 1, b: 2, c: 3 }],
    ['multi_media', '/:type/*anything/:id']
  ]);
 
  // via the `add` method
  router.add(['single_media', '/:type/single/:id', { x: true, y: false }]);

Matching

  router.hasMatch('/post/123');
  //=> true
 
  router.hasMatch('/cats');
  //=> false
  router.match('/post/123');
  //=> { name: 'post', meta: {a: 1, b: 2, c: 3}, params: {id: '123'}}
 
  router.match('/cats');
  //=> null

Make path

  router.makePath('post', {id: '456'});
  //=> '/post/456'
 
  // missing required params
  router.makePath('post');
  //=> null
 
  // non-existing route
  router.makePath('cats');
  //=> null

Keywords

none

install

npm i route-builder

Downloadsweekly downloads

39

version

1.2.0

license

none

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability