npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

radix-router

v3.0.1

Published

Radix tree based router

Downloads

88,298

Readme

Radix Router

Greenkeeper badge

Build Status Coverage Status

A fast, simple path router that is optimized for consistently fast lookups. This router has support for placeholders and wildcards.

Installation

npm install --save radix-router

Usage

A minimal example:

const RadixRouter = require('radix-router')

const router = new Router()
router.insert({
  path: '/api/people/:id',
  data: { some: 'data' }
})

const { data, params } = router.lookup('/api/people/123456')

const { id } = params
console.log(id) // prints: '123456'
console.log(data) // prints: { some: 'data' }

Creating a new Router

new RadixRouter(options) - Creates a new instance of a router. The options object is optional.

Possible parameters for the options object:

  • routes - The routes to insert into the router.
  • strict - Setting this option to true will force lookups to match exact paths (trailing slashes will not be ignored). Defaults to false.
const RadixRouter = require('radix-router')

const router = new RadixRouter({
  strict: true,
  routes: [
    {
      path: '/my/api/route/a', // "path" is a required field
      // any other fields will also be stored by the router
      extraRouteData: {},
      description: 'this is a route'
    },
    {
      path: '/my/api/route/b',
      extraRouteData: {},
      description: 'this is a different route',
      routeBSpecificData: {}
    }
  ]
})

Router methods

insert(routeData)

Adds the given data to the router. The object passed in must contain a path attribute that is a string. The path will be used by the router to know where to place the route.

Example input:

router.insert({
  path: '/api/route/c', // required
  // any additional data goes here
  extraData: 'anything can be added',
  handler: function (req, res) {
    // ...
  }
})
lookup(path)

Performs a lookup of the path. If there is a match, the data associated with the route is returned, otherwise this will return null.

Usage:

const routeThatExists = router.lookup('/api/route/c')

Example output:

{
  path: '/api/route/c',
  extraData: 'anything can be added',
  handler: function (req, res) {
    // ...
  }
}
remove(path)

Removes the path from the router. Returns true if the route was found and removed.

Usage:

const routeRemoved = router.remove('/some/route')
startsWith(path)

Returns a map of all routes starting with the given prefix and the data associated with them.

Usage:

const apiRoutes = router.startsWith('/api')

Example output:

[
  {
    path:'/api/v1/route',
    much: 'data'
  },
  {
    path: '/api/v1/other-route/:id',
    so: 'placeholder',
    much: 'wow'
  }
]

Wildcard and placeholder matching

Wildcards can be added by to the end of routes by adding /** to the end of your route.

Example:

router.insert(
  path: '/api/v2/**',
  such: 'wildcard'
})

Output of router.lookup('/api/v2/some/random/route'):

{
  path: '/api/v2/**',
  sucn: 'wildcard'
}

Placeholders can be used in routes by starting a segment of the route with a colon :. Whatever content fills the position of the placeholder will be added to the lookup result under the params attribute. The name given for the placeholder in the path is the key to retrieve the parameter from.

Example:

router.insert(
  path: '/api/v2/:myPlaceholder/route'
})

router.insert(
  path: '/api/v3/:organizations/directory/:groupId'
})

Output of router.lookup('/api/v2/application/route'):

{
  path: '/api/v2/:myPlaceholder/route',
  params: {
    myPlaceholder: 'application'
  }
}

Output of router.lookup('/api/v3/test-org/directory/test-group-id'):

{
  path: '/api/v3/:organizations/directory/:groupId',
  params: {
    organizations: 'test-org',
    groupId: 'test-group-id'
  }
}