0.2.5 • Published 4 years ago

routing-buddy v0.2.5

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

routing-buddy

Yet another routing library in JavaScript. This project was created after looking at and trying several JavaScript routing libraries out there and not finding one that really worked that way I wanted it to work. I wanted a library that could be used both on the client and server side without feeling like it was designed for one side with the other bolted on like it was an afterthought. I also wanted a library that gave more flexibility for defining routes.

Status

NOTE: This is an archived project. There is no roadmap for updates. There may be updates to dependencies for security updates.

Design

The main class exported in this library is Router. Router is a wrapper around RouteNode. A RouteNode is a tree structure to define all routes.

For development

To run unit tests:

$ # run unit tests and lint
$ npm test
$ # run unit tests only
$ mocha
$ # run linter only
$ eslint

See unit tests for more details.

Usage:

var Router = require('routing-buddy')

var router = new Router()

// Define a handler to be called when routed to the url
router.add('/some/path/here', function () {
  // do something for '/some/path/here'
})

// Get the handler for '/some/path/here' and call it.
// Call the callback after finding the route
router.route('/some/path/here', function (err, maybeResults) {
  // `err` will be either null or Error.
  //    null: everything was fine
  //    Error: missing route or any other error that might happen
  // maybeResults: The return value from the hander from add
})

// add routes with regex
router.add(['/by/regex', /(\d+)/g], function (one, two, three) {
  // one === '1', two === '2', three === '3'
})
router.route('/by/regex/1-2-3')

function toInt(part) {
  var results = /^(\d+)$/.exec(part)
  return results ? parseInt(results[0], 10) : null
}

// add routes with function
var router = new Router()
router.add(['by', 'func', toInt], function (val) {
  // val === 123
})

router.route('/by/func/123')

// Can check for not found route
router.route('/unknown/route', function (err) {
  // Router.isNotFound(err) === true
})

// Can pass arguments to route handler
router.add(['/with/args', toInt], function (someObj, param) {
  // someObj = {some: 'obj'}
  // param === 123
})
router.route('/with/args/123', {args: [{some: 'obj'}]})
0.2.5

4 years ago

0.2.4

4 years ago

0.2.3

4 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

8 years ago

0.1.2

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago