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

keen-router

v0.2.1

Published

A tree based, disambiguating path router (for HTTP)

Downloads

6

Readme

Keen Router

Build Status Depdendency Status Code Coverage

Introduction

This is a path router for use with your favorite web framework.

It is a bit special because:

  • It uses a tree based structure to store routes, it doesn't use a list like most popular routers
    • It will use as few comparisons as possible to determine if a path is matched
  • It can disambiguate between multiple conflicting routes, and will choose the best one

Routing logic

Routes are stored in a tree structure, so the moment a path element doesn't match we stop comparing routes. So let's imagine the following routes, in our router:

  • /foo/bar
  • /foo/:param
  • /foo/:param/bar
  • /foo/:param/baz

A tree structure like so is created in the router:

  • foo
    • bar
    • :param
      • bar
      • baz

It uses a depth first search upon a tree to match routes, branching the search when multiple possible routes are encountered. It should perform better then a router which uses a linear search method, especially when there are many branches in the route. But the primary value isn't speed, it is the ability to disambiguate between multiple conflicting routes.

The router will prefer exact path element matches over parameter matches, but will still consider the parameter matches.

So in the case where we would have multiple possible matches:

  • /a/:1/:3/:4
  • /:1/b/:2/:3
  • /:1/:2/c/:3
  • /:1/:2/:3/d

where a path like:

/a/b/c/d

Could match any of the above paths, the router will choose the route which was added first.

Examples

Here is a very simplistic example, where we define multiple routes

  var Router = require('keen-router');

  var r = new Router();

  //We'll attach some arbitrary data to this route
  r.add("/user", { call: "User.create" });
  r.add("/account");
  r.add("/mailbox");
  r.add("/user/:id");

  r.resolve("/user"); // returns { route:"/user", params:{}, data: { call: "User.create"}});
  r.resolve("/user/55"); // returns { route:"/user/:id", params:{id:"55"}});

  //Later we could decide to remove a route:
  r.remove("/user");

  r.list(); //returns ["/account","/mailbox","/user/:id"]
  

Here is an example which uses call backs:

  var Router = require('keen-router');

  var r = new Router();
  
  // the :bar is a parameter and will be put into the params hash
  r.add("/foo/:bar",function(route,params){
    //route would contain the route if matched, i.e. /foo/:bar
    //params would contain the matched parameters, i.e. { bar:"bar"}
  });

  r.resolve("/foo/bar");

You can also specify your own tokenizer for routes:

  
  var Router = require('keen-router');

  var r = new Router(function(path){
    return path.split("|");
  });

This means that you can support routes in various formats such as:

METHOD HOSTNAME PATH

such as

GET foo.com /user