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-route-tree

v3.0.0

Published

The most powerful route middleware for koa1, koa2, for build your route quickly with a little configuration

Downloads

28

Readme

koa-route-tree

Path is route, also path is parameter.
The most fast and efficient route.
Now, it support restful route mode.

Let's see it.

Install

npm install koa-route-tree --save

Caution!

This is only support for koa@2+!

If you use koa@1, install it with version 1.4.0. npm install [email protected] --save

Usage

// File: app.js

const Koa = require('koa');
const Route = require('koa-route-tree');

const app = new Koa();

const fileRouter = ['robots.txt'];
app.use(Route(__dirname + '/controller', {
    '/appList': '/app/list',
    'favicon.ico': '/index/favicon.ico'
}, async function (ctx, next, controller) {
    let pathname = ctx.path.substring(1);
    
    if (fileRouter.indexOf(pathname) > -1) {
        ctx.body = 'Allows: *';
        return;
    }
    
    // console.log(controller);
    // and do next if you want
    // await next();
}));

Restful mode example

// File: controllers/app/list.js

/**
 * Normal Get Request, Support urls:
 * 1. /app/list => page: undefined | second: undefind
 * 2. /app/list/0 => page: 0 | second: undefined
 * 4. /app/list/1/a => page: 1 | second: a
 */
exports.index = function (page, second) {
    this.body = 'Page/' + page + '/Second/' + second;
};

// restful route mode
/**
 * For GET Request. Support urls:
 * 1. GET /app/list/set => page: undefined | second: undefind
 * 2. GET /app/list/0/set => page: 0 | second: undefined
 * 3. GET /app/list/1/set/a => page: 1 | second: a
 * 4. GET /app/list/set/1/a => page: 1 | second: a
 */
exports.set = function (page, second) {
    this.body = 'GET Page/' + page + '/Second/' + second;
};

/**
 * The same as the above GET request
 */
exports.postSet = function (page, second) {
    this.body = 'POST Page/' + page + '/Second/' + second;
};
/**
 * The same as the above GET request
 */
exports.putSet = function (page, second) {
    this.body = 'PUT Page/' + page + '/Second/' + second;
};

Class

Route(controllerDirectory[, alias][, withoutRouteHandler])

Parameters

  • controllerDirectory is a string of controller directory.

  • alias is a object for alias to a path or function.

    const alias = {
        '/d': '/index/download', // a short download link
        'a/b': 'your/path' // the same link, this is equivalent to /a/b <=> /your/path
    };
  • withoutRouteHandler *(ctx, next, controller) is a middleware function for handle the request without route.

    So the context of the function is the current koa context.

Static Attribute

route.controller is a freeze object like a tree. And the value must be a function at the end of the tree.

Controller

  • index is default path.

    // File: app.js
      
    // GET /app
    exports.index = function () {
        this.body = 'app';
    };
    exports.index = function (id) {
        this.body = id || 'app';
    };
  • Http method is a prefix of function name but GET.

    // File: index.js
      
    // GET /
    exports.index = function (id) {
        this.body = id;
    };
      
    // POST /
    exports.postIndex = function (id) {
        this.body = id;
    };
      
    // PUT /
    exports.putIndex = function (id) {
        this.body = id;
    };
      
    // DELETE /
    exports.deleteIndex = function (id) {
        this.body = id;
    };

Test

./node_modules/.bin/mocha
# or
npm test # npm run test

The End

Anyway, try to use. And see the example in example directory.

Hope this is useful to you.

And give me your pull request if you have any improvements or suggestions.