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-66

v1.0.1

Published

Router middleware for koa v2

Downloads

1,715

Readme

koa-66

Node.js Version NPM version build status Dependency Status Coverage Status

Router middleware for koa v2.

feedbacks are welcome

Features

  • Plugin middleware support
  • Express like http verbs methods (including all)
  • Express like use function
  • Express like param function
  • Automatic OPTIONS response
  • Automatic HEAD when GET is present
  • 501 and 405 status (throw capability with headers)
  • Mount instance on specific path
  • Multiple middleware as arguments
  • Multiple middleware as array

Installation

# with npm
$ npm install koa-66

$ with yarn
$ yarn add koa-66

Example

const Koa = require('koa');
const Router = require('koa-66');
const app = new Koa();

const router = new Router();
const mainRouter  = new Router();

router.param('id', (ctx, next, id) => {
        ctx.yolo = id;
        return next();
});

router.use(async function(ctx, next) {
    ctx.a = " ";
    await next();
});

router.get('/:id', (ctx, next) => {
    return next().then(() => {
        ctx.body += ctx.a + ctx.yolo;
   })
});

router.get('/:id', async function(ctx) {
    ctx.body = await Promise.resolve('hello');
});

mainRouter.mount('/pouet', router);

app.use(mainRouter.routes());

app.listen(1664);
// GET http://localhost:1664/pouet/world
// => hello world

Example with throw option

const Koa = require('koa');
const Router = require('koa-66');
const app = new Koa();

const router = new Router();

app.use(async function(ctx, next) {
	try {
		await next();
	}catch(e){
		if (e.status === 405) {
			ctx.status = 405;
			ctx.set(e.headers);
		}
	}
})

router.get('/', (ctx) => ctx.body = 'hello');

app.use(router.routes({throw: true}));

app.listen(1664);

// > curl http://localhost:1664/ -I -X POST
//
// HTTP/1.1 405 Method Not Allowed
// allow: HEAD, GET
// Content-Type: text/plain; charset=utf-8
// Content-Length: 18
// Date: Wed, 04 Nov 2015 10:29:06 GMT
// Connection: keep-alive

Plugin support

I don't know if Plugin is a good term for this feature. The goal was to add cappability to register some middleware on a main Router that will be inject via config object on different route. (ex: authentication or acl behaviour). Why? Because I am lazy to require some middleware in all my router script with generaly relatif path...

So I decided to add the possibility to inject an object at first parameter (that will be a config object) and adding an extra middleware that will be inject in middleware stack. To register this plugin just use a plugin()method.

const Router = require('koa-66');
const main = new Router();

// you can use multiple middleware as arguments or array
main.plugin('authent', (ctx, next) => {
    // pick plugin config object on ctx.state.plugins.
    // here ctx.state.plugins.authent === true;
	// do stuff inject user on context for example
	return next();
	//or throw or do nothing that will stop execution of router stack
})

main.plugin('acl', (ctx, next) => {
    // here ctx.state.plugins.acl === ['admin'];
	// do stuff check role via ctx.state.plugins.acl for example
	return next();
	//or throw or do nothing that will stop execution of router stack
})

const router = new Router();

router.use({authent: true});
//options here is a boolean,
//but you can pass everything you want,
//and it will be inject as options

router.get('/private', {acl:['admin']}, 
	ctx => ctx.body = 'private'
)

main.mount('/api', router);
...	
// order of call /api/private
// 1 plugin authent
// 2 plugin acl
// 3 real middleware 

Test

# npm test