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

fast-koa-router

v1.4.2

Published

It uses a simple routes json object. Routes order does not matter. Performance does not degrade with routes length.

Downloads

1,385

Readme

Fast Koa Router

It uses a simple routes json object. Routes order does not matter. Performance does not degrade with routes length.

Installation

npm install fast-koa-router

About

Usage

const routes = {
  get: {
    '/path': async function (ctx, next) {
      ctx.body = 'ok';
    },
    '/nested': {
      '/path': async function (ctx, next) {},
      '/path/:id': async function (ctx, next) {}
    }
  },
  post: {
    '/path': async function (ctx, next) {}
  },
  policy: {
    '/path': async function (ctx, next) {},
    '/nested': {
      '/path': async function (ctx, next) {},
      '/path/:id': async function (ctx, next) {}
    }
  },
  prefix: {
    '/': async function (ctx, next) {} // / matches all routes
  }
};

// or

const routes = {
  '/path': {
    get: async function (ctx, next) {
      ctx.body = 'ok';
    },
    post: async function (ctx, next) {},
    policy: async function (ctx, next) {}
  },
  '/nested': {
    '/path': { get: async function (ctx, next) {}, policy: async function (ctx, next) {} },
    '/path/:id': { get: async function (ctx, next) {}, policy: async function (ctx, next) {} }
  },
  prefix: {
    '/': async function (ctx, next) {} // / matches all routes
  }
};

// or

const routes = {
  get: {
    '/path': async function (ctx, next) {},
    '/nested/path': async function (ctx, next) {},
    '/nested/path/:id': async function (ctx, next) {}
  },
  post: {
    '/path': async function (ctx, next) {}
  },
  policy: {
    '/path': async function (ctx, next) {},
    '/nested/path': async function (ctx, next) {},
    '/nested/path/:id': async function (ctx, next) {}
  },
  prefix: {
    '/': async function (ctx, next) {} // / matches all routes
  }
};

Supports

  • put
  • post
  • get
  • delete
  • patch

methods

const Koa = require('koa');
const app = new Koa();
const { router } = require('fast-koa-router');

app.use(router(routes));
app.listen(8080);

Debugging in console

node
> const { router } = require('fast-koa-router');
> const route = router(routes);
> route.matching('/nested/path');
{
  ctx: {
    path: '/nested/path',
    method: 'GET',
    params: {},
    _matchedRoute: '/nested/path'
  },
  middlewares: [
    [AsyncFunction: '/nested/path'], // policy
    [AsyncFunction: '/'],            // prefix route
    [AsyncFunction: '/nested/path']  // get route
  ]
}
> route.routes
// contains the compiled routes

Star symbol

Sometimes you need to have a fallback if no route matches with the requested url. You can have routes that end with a star eg:

const routes = {
  get: {
    '/path': async function(ctx, next) {},
    '/path/subpath': async function(ctx, next) {},
    '/path/*': async function(ctx, next) {
      ctx.body='Nothing in /path matches this request';
      ctx.status=404;
    },
    '/*': async function(ctx, next) {
      ctx.body='Nothing here';
      ctx.status=404;
    }
  }

Note that star symbol is only supported after version 1.1.0 and only when used in the end of a route.

There is no reason to use it in prefix routes. Prefix routes will always match get, post, delete, patch, put urls if they use the same prefix.

Variables

You can use variables in paths eg

const routes = {
  get: {
    '/path/:id': async function (ctx) {
      ctx.body = ctx.params.id;
    }
  }
};

If you hit the url /path/1 the ctx.params.id will be equal to 1. Using different variable names in similar paths is discouraged eg:

const routes = {
  get: {
    '/path/:id': async function (ctx) { ctx.body = ctx.params.id;  }
    '/path/:differentId/foo': async function (ctx) { ctx.body = ctx.params.id;  }
  }
};

In such cases both ctx.params.id and ctx.params.differentId will be set. If there is a conflict in names with a variable used later on then the variable that comes later in the path has priority:

const routes = {
  get: {
    '/path/:id/:id': async function (ctx) {
      ctx.body = ctx.params.id;
    }
  }
};

// or 
const routes = {
  get: {
    '/path/:id': async function (ctx) { ctx.body = ctx.params.id;  }
    '/path/:differentId/:id': async function (ctx) { ctx.body = ctx.params.id;  }
  }
};

If you hit the url /path/1/2 the ctx.params.id will equal to 2. Again such usage is discouraged

Policies

Policies are used to add authentication and authorization or any other kind of middleware. It is like all and is executed before the matching route. They must call next in order for the actual route to be executed. Policies will be executed even if a matching get, post, put, delete, patch is not found Policies are executed before anything else.

Prefixes

Prefixes are also used to add middleware. Unlike policies they will only be executed if a matching get, post, put, delete or patch is found. They are convenient to add authentication or authorization in many paths that start with a prefix eg: /api/v1

Prefixes are executed after policies and before other middleware. Note than both in prefix and policy middleware ctx.params and ctx._matchedRoute are available.

Fast

The path matching is pretty simple. Unlike other middlewares not all routes are checked so performance does not degrade with routes size. However complex regex matching is not supported.

Benchmark

Performances tests existing in this codebase and comparing fast-koa-router with @koa/router.

To start fast-koa-router example:


node performance-test/router/server.js

To start @koa/router example:


node performance-test/koa-router/server.js

Metrics have been taken using ab:


ab -k -n 1000000 -c 100 localhost:8080/api/v1/1/2

image