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

koa-express-router

v0.3.4

Published

Express's Router adapted for Koa v2.x

Downloads

11

Readme

koa-express-router

Express's Router adapted for Koa v2.x

NPM version node version NPM Downloads Build Status License

  • Express-style routing using router.use, router.all, router.METHOD, router.param etc
  • Support router prefix
  • Support query matching

Thanks To

Installation

npm install koa-express-router

Usage

Basics

Just like express

Differences
  • use new to create instances
  • use .routes() or .routes(false) to export a router
const Koa = require('koa');
const Router = require('koa-express-router');

// change default settings
Router.defaultOptions.mergeParams = true;

// define sub router with an optional prefix
// NOTE: different from Express: use new to create instances
const subRtr = new Router({ prefix: '/sub' });

subRtr.use(async (ctx, next) => {
  if (/* some condition */false) {
    return next('router');  // possible to skip the current router
  }
  ctx.body += '/sub use \n';
  return next();
});

subRtr.route('/list')
  .all(async (ctx, next) => {
    if (/* some condition */false) {
      return next('route');  // possible to skip the current route (goto #1)
    }
    ctx.body += '/sub/list all 1\n';
    return next();
  })
  .get(async (ctx, next) => {
    ctx.body += '/sub/list get\n';
  })
  .post(async (ctx, next) => {
    ctx.body += '/sub/list post\n';
  });

subRtr.all('/list', async (ctx, next) => {
  // #1
  // the logic would come here
  ctx.body += '/sub/list all 2\n';
});

// define top router without a prefix
const topRtr = new Router();
topRtr.use((ctx, next) => {
  ctx.body = 'global use\n';
  return next();
});
topRtr.param('someID', async (ctx, next, id, name) => {
  // id: matched value
  // name: 'someID'
  // ...
  ctx.body += `/top/:someID ${name} => ${id}\n`;
  return next();
});

// NOTE: different from Express: use .routes() to export
// no need to pass arguments to .routes()
//   if .routes() is to be used by a Router from 'koa-express-router'
topRtr.use('/top/:someID([1-9]{1}[0-9]{0,})/', subRtr.routes());

const app = new Koa();
// pass 'false' when .routes() may be used
//   by app.use, compose, or something other than a Router from 'koa-express-router'
app.use(topRtr.routes(false));
app.listen(3000);

Output

  • GET /
    global use
    
  • PATCH /top/10/sub/ (does not match any path in subRtr)
    global use
    /top/:someID someID => 10
    /sub use
    
  • GET /top/bad/sub/list (does not match the use for subRtr.routes())
    global use
    
  • GET /top/2/sub/list
    global use
    /top/:someID someID => 2
    /sub use
    /sub/list all 1
    /sub/list get
    
  • PUT /top/3/sub/list
    global use
    /top/:someID someID => 3
    /sub use
    /sub/list all 1
    /sub/list all 2
    

Query Matching

Note: this feature is only designed for simple matching. If the matching condition becomes complex, it is recommended that the user consider return next('route') for better readability. See below.

// use an object for query matching schema
// the value part can be a primitive
router.use({ type_id: 1, state: 'good' }, async (ctx, next) => {
  // matched on ?type_id=1&state=good
  return next();
});

router.route('/list')
  // the value part can be a function
  .get({ user_id: val => Number(val) < 1000 }, async (ctx, next) => {
    // case ?user_id=30
    // ...
    // use next('route') to break (like break in switch-case statements), if needed
    return next('route');
  })
  // the value part can be a regexp
  .get({ user_id: /00$/ }, async (ctx, next) => {
    // case ?user_id=3000
    // ...
    // use next('route') to break
    return next('route');
  })
  // the value part can be a string array (meaning or)
  .get({ user_id: ['2222', '3333'] }, async (ctx, next) => {
    // case ?user_id=2222 or ?user_id=3333
    // ...
    // use next('route') to break
    return next('route');
  })
  .get(async (ctx, next) => {
    // default case
    return next('route');
  });

// complex query condition example
router.post('/',
  async (ctx, next) => {
    const authorized = await checkAuthorized(ctx.query.user_id);
    if (authorized) return next(); // goto #1
    return next('route'); // goto #2
  },
  async (ctx, next) => {
    // #1
    // authorized operations
  },
);

router.post('/',
  async (ctx, next) => {
    // #2
    // limited operations due to not being authorized
  },
);

Caveats

  • Not ready for production use
  • Not support other less common HTTP methods
  • Not benchmarked

License

MIT