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-router-bridge

v1.0.7

Published

Easy bridge for koa-router

Downloads

15

Readme

Koa router bridge: easy way to make bridges for routes.

This plugin extends standart Koa router and allow to make simple and clean constructions to provide nested routes.

Usage:

import bridgeRouter from 'koa-router-bridge';   //-- import the module
import koaRouter from 'koa-router';   // import standart koa-router
let bridgedRouter = new bridgeRouter(koaRouter);   // patch the koa-router
// make new router
let router = new bridgedRouter();   

Now we have standart router with bridge extension. This is very helpful to define complete routes structure with using different middleware checks, nested structures and other. Let use it!

// get our routes structure (would be explained later)
import routes from './app/routes';  
// set some uses
router.use(async (ctx, next)=> {
    ctx.router = router;    //-- why not? ;)
    await next();
})

//-- let bridge it!
router.bridge(routes.init, (router) => {    // first use: cover all futher requests with specific init 
    //-- router is standart koa-router object everywhere and has every koa-router methods, plus .bridge(..)
    router.get('/', routes.index);

    router.bridge('/blog', [routes.blog.init], (router) => { //-- list of routes is allowed too!
        router.get('/', routes.blog.index);
        //-- request url is /blog/:blogId
        router.bridge('/:blodId', routes.blog.checkId, (router) => {
            router.get('blogView', '/', router.blog.view);
            // easy way to  determine different output formats
            router.get('.json', router.blog.itemJson);  // /blog/:blogId.json
            router.get('.xml', router.blog.itemXML);    // /blog/:blogId.xml
            router.get('.html', router.blog.itemHTML);  // /blog/:blogId.html
            //-- request url is /blog/:blogId/comments
            router.bridge('/comments', (router) => {
                router.get('postComments', '/', router.blog.comments); //-- store url for router.url('postComments');
                router.post('/add', router.blog.addComment);
            });
        });
    });

    /**
    Every nested request is protected by chain of middleware routes
    Keep your API in safety! 
    */

    router.bridge('/shop', routes.shop.init, (router) => {
        router.get('/', routes.shop.index);
        router.bridge('/:itemId', routes.shop.checkItem, (router) => {
            //-- request url is /shop/:itemId
            router.get('/', routes.shop.viewItem);
            //-- request url is /shop/:itemId/order
            router.post('/order', routes.shop.orderItem);
        });
    });
});

/** attach complete router to your Koa app */ 
app.use(router.routes()).use(router.allowedMethods());

Job complete! Now your router has complete path definitions for every nested elements, and for each of them will be executed necessary chains of conditions, data setting and other preparations.

Everything depends on routes functions. Let's explain them.

./app/routes.js

let router = {
    /** for koa 1.* allowed generators and yield */
    /** for example the base middleware must check for trailing slash into url */
    init : async (ctx, next) => {
        let url = ctx.req._parsedUrl;
        let pathname = url.pathname;
        var checkExt = /.*[\.(xml|html|json)]$/; // requests with extensions must been skipped
        if (pathname.search(checkExt)<0 && pathname.substr(-1)!=='/') {
            ctx.redirect((pathname+'/')+(url.search||''));
            return;
        }
        //-- add some through object e.g. 
        ctx.$$ = {};
        //-- then go deeper
        await next();
    },
    // index router, just render index template
    index: async (ctx, next) => {
        await ctx.render('index', {});
    },
    // let init blog section
    blog: {
        // init function must prepare some object for futher usage
        init: async (ctx, next) => {
            //--  store Blog for next usage
            ctx.$$.Blog = new Blog();
            await next();
        },
        // then we can use this object for specific actions
        index: async (ctx, next) => {
            let data = await ctx.$$.Blog.getList({...});
            await ctx.render ('blog/index', {... data ...});
        },
        // middleware before every request for /blog/:blogId 
        checkId: async (ctx, next) => {
            var blogItem = await ctx.$$.Blog.getItem(ctx.params.blogId);
            if (blogItem) {
                ctx.$$.blogItem = blogItem;
                await next();
            } else {
                ctx.throw (404);
            }
        },
        // viewItem would been executed if only `checkId` returns next();
        viewItem: async (ctx, next) => {
            await ctx.render('blog/item', {item: ctx.$$.blogItem, url: ctx.router.url('blogView', ctx.params)});
        },
        // the same for every specific output format
        itemJSON: async (ctx. next) => {
            ctx.json(ctx.$$.blogItem);
        },
        //... other code 
    },

    //-- same methods for shop init
    shop: {
        init: async (ctx, next) => {
            ctx.$$.Shop = new Shop();
            await next();
        },
        //... code for other methods 
    },
    // also we can require some extensions from other files
    other: require ('./other'),
}
// export our router
module.exports = router;

Also we can import routes functions from specific files or write code right into router. This is not forbidden :)

import blogRoutes from './Routes/blog';
import shopRoutes from './Routes/shop';
/** ... */
router.bridge('/blog', blogRoutes.prepare, (router)=> {
    router.get('/', blogRoutes.index);
    router.bridge('/:blogId', blogRoutes.check, (router) => {
        router.get('/', blogRouter.view);
        router.post('/save', blogRouter.save);
    });
});
/** ... */
router.bridge('/shop', shopRoutes.prepare, (router) => {
    // ...
});
// write the code into bridges 
router.bridge('/user', async (ctx, next) => { console.log ('do users preparation'); await next(); }, (router) => { 
    router.get('/', async(ctx, next) => { ctx.render('users/index', {} });
});

P.S.:

koa-router-bridge may have some magic with roudex ;)