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

chestnut-router

v0.2.4

Published

for chestnut-app

Downloads

2

Readme

chestnut-router Koa2快速路由

配合chestnut-app使用,也可单独在Koa2中使用

用法

app.js


const Koa = require('koa');

const path = require('path');

const router = require('chestnut-router');

const app = new Koa();

// 应用的根目录
const rootPath = path.join(__dirname);

app.use(router(rootPath));

这时候需要在router代码中使用router.create方法来使用路由。

静态方法

create 创建路由

create方法接受两个参数,第一个是父目录,第二个是过滤器数组

routers目录下的路由定义:


// 返回的是koa-router对象,代表父目录为/interface,如果是根目录使用/或者空串
const router = require('chestnut-router').create('/interface');
// 使用过滤器写法
// const router = require('chestnut-router').create('/interface', ['session']);

module.exports = router
  .get('/login', async function(ctx){
		ctx.body = 'hi chestbut app';
	}); // 访问路径为http://ip:port/interface/login

addFilters 添加过滤器

addFilters接受一个参数,为过滤规则对象,形如:{key : {handler: function(ctx){}}} 或者 {key: function(ctx){}}

当函数返回false则所有中间件全部处理结束,如果返回null则所有过滤器处理结束,返回其他值或者不返回则后续过滤器和中间件继续处理

比如:


const router = require('chestnut-router');

router.addFilters({
	session : {
        handler : function(ctx){
            const hasData = ctx.session.hasData;
            if(!hasData) ctx.body = {
                result : 'fail',
                msg : '会话超时'
            };
            return hasData;// 没有会话时返回false,后续过滤器和中间件不处理
        }
    },
	fliter1 : function(ctx){
		console.log('进入了过滤器');
	}
	
});

过滤器可以在create中使用,也可以在excuteFiters中使用

使用的时候过滤器数组元素必须为过滤器的key

excuteFiters 执行过滤器

excuteFiters接受一个参数,此次参数为过滤器数组,数组的元素必须是addFilters方法添加的规则的key或者直接是一个function(ctx){}函数,此函数规则跟addFilters中要求的函数有一致的要求

在koa中间件中使用


const router = require('chestnut-router');

app.use(router.excuteFiters(['session', function(ctx){
	console.log('经过了临时过滤器');
}]));

在路由创建时使用


const router = require('chestnut-router').create('/interface', ['session']);

在路由定义时使用


// 返回的是koa-router对象,代表父目录为/interface,如果是根目录使用/
const router = require('chestnut-router').create('/interface');

module.exports = router
  .get('/login', router.excuteFiters(['session'], async function(ctx){
		ctx.body = 'hi chestbut app';
	}); // 访问路径为http://ip:port/interface/login

使用自定义过滤器


const router = require('chestnut-router');

const custom = function(ctx){
	// do sth.
};

app.use(router.excuteFiters(['session', custom]));

链式路由

有的时候需要将一组url进行统一控制使用过滤器,可以使用链式路由。

即使用router的add函数来添加子路由。

add函数支持任意多个参数,每个参数必须符合下列条件之一:

1.入参router对象

2.入参字符串,其代表某个存在的本地目录路径,该路径下的所有js文件对应的模块必须是router对象并全部作为子路由添加进来。

比如:


const routerMiddleware = require('chestnut-router');

// 创建主路由
const router = routerMiddleware.create('/interface');
// 创建子路由
const childRouter = routerMiddleware.create('/');

// 要求childRouterPath下的所有js文件作为模块引入时为router对象
const childRouterPath = path.join(__dirname, '../../childs');

// 子路由创建请求
childRouter
    .get('child', async function(ctx, next){
        ctx.body = '使用了子路由的响应';
    });

module.exports = router.add(childRouter, childRouterPath);