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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@jtarchie/some-router

v0.0.5

Published

router for http requests that is platform agnostic

Readme

some-router

A framework independent routing in Javascript. It will match params, globs, and static routes. It does not invoke the handler, just returns it.

Influenced by my experience in Sinatra Ruby framework. Motivation to have generic router that can be used across different platforms -- HTTP, Cloudflare Workers, etc.

Usage

This library supports several different routing primitives:

  • MethodRouter supports loading routes based on HTTP verbs (GET, POST, etc.). It does not make assumptions where the request meta-data comes from, so has to be called explicitly.

    import { MethodRouter } from "some-router";
    
    const router = new MethodRouter();
    router.get("/", function () {
      return ("/");
    });
    router.get("/a", function () {
      return ("/a");
    });
    
    const { callback } = router.find("GET", "/");
    console.assert(callback() == "/");
  • HTTPRouter supports the functionality from MethodRouter. It loads information of request path and HTTP verb from the HTTP request object.

    const http = require("http");
    
    const host = "localhost";
    const port = 8000;
    const router = new HTTPRouter();
    
    router.get("/persons/:name", ({ params, response }) => {
      response.writeHead(200);
      response.end(`Hello, ${params.name}`);
    });
    
    const requestListener = function (req, res) {
      router.lookup(request, response);
    };
    
    const server = http.createServer(requestListener);
    server.listen(port, host, () => {
      console.log(`Server is running on http://${host}:${port}`);
    });
  • EventRouter supports FetchEvent applications. It was designed to be used with CloudFlare Workers.

    const router = new EventRouter();
    router.get("/persons/:name", ({ params }) => {
      return new Response(null, {
        status: 200,
      });
    });
    
    export default {
      async fetch(request) {
        return await router.handle(request);
      },
    };

Route Types

  • Static - This is an exact string match.

    import { MethodRouter } from "some-router";
    
    const router = new MethodRouter();
    router.get("/", () => {});
  • Named Parameter - This support dynamic content, which will match against a regex wildcard (.*?). The parameters are returned in a params if a matching route is found as string values.

    import { MethodRouter } from "some-router";
    
    const router = new MethodRouter();
    router.get("/persons/:id/children/:child_id", () => {});
    
    const { params } = router.find("GET", "/persons/1/children/100");
    console.assert(params == { id: "1", child_id: "100" });
  • Regexes - This supports dynamic content that needs to fit a specific regex matcher. The regexes must be represented in a capture group and the values will be returned as regexN in index 0 placement of the capture group.

    Named capture groups are also supported. The name on of the group will be placed as is in params.

    No effort is done to ensure regexes are proper, valid, etc. If the route isn't matching the regex is most likely wrong.

    NOTE: Proper escaping of back slash is required. This is because of javascript string encoding.

    import { MethodRouter } from "some-router";
    
    const router = new MethodRouter();
    router.get("/persons/(\\d+)/children/(?<child_id>\\w+)", () => {});
    
    const { params } = router.find("GET", "/persons/1/children/100");
    console.assert(params == { regex0: "1", child_id: "100" });

Precedence

When defining routes, the order of declaration does not matter. The evaluation of routes affects the order.

Listed in order of precedence:

  • exact match of a static route
import { MethodRouter } from "some-router";
const router = new MethodRouter();
router.get("/a", function () {
  return ("static");
});
router.get("/(\w+)", function () {
  return ("dynamic");
});

const { callback } = router.find("GET", "/a");
console.assert(callback() == "static");
  • longest matching route based on minimize size of matcher (complexity)
import { MethodRouter } from "some-router";
const router = new MethodRouter();
router.get("/:first-:second", () => {});
router.get("/:first", () => {});

const { params } = router.find("GET", "/a-b");
console.assert(params == { "first": "a", "second": "b" });

Development

For development, the deno tool is used for linting and formatting.

On Mac OS, we've included assistance for development.

brew bundle
yarn install
yarn run test