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

v1.0.2

Published

基于koa-router中间件基础之上的一个扩展版本,把路由文件跟控制器独立开来,写法类似于Sails的routes.js文件。并且实现了根据目录路径自动注册路由。

Downloads

2

Readme

koa-router-pro

koa-router-pro v1

[email protected] 是基于koa-router中间件基础之上的一个扩展版本,把路由文件跟控制器独立开来,写法类似于Sails的routes.js文件。并且实现了根据目录路径自动注册路由。

var routerPro = require('koa-router-pro');
var router = routerPro({//这里会返回koa-router的实例
  root:__dirname+path.sep+'app'+path.sep+'controllers'+path.sep,//controller的根目录
  routes:[{routes:{'get /' : 'IndexController.test','post /' : '/user/InfoController.test'},prefix:'/v1',name:'user::'}],//一个数组每个元素是一个包含routes,name,prefix的对象prefix和name为可选项
  prefix:'/v1',//路由前缀,可选项
  autoLoad:false//自动路由的配置,会根据配置的root目录来自动注册目录及其子目录下的所有控制器文件,默认是true。如果设置为false就会关闭。

});
app.use(router.routes());

通常把routes配置选项通过rquire路由文件来获得,比如var routes = rquire('routes.js');。 可以把routes.js路由文件拆分成多个路由文件比如userRoutes.js,adminRouter.js然后配置到routes数组选项中去

module.exports.prefix = '/user';//路由文件中的路由前缀,可选项
module.exports.name = 'admin::';//路由文件的name属性,如果路由中用了name属性并假设至为login,则可通过router.url('admin::login')来获得路由地址,可选项
module.exports.routes = {
  '/info':function *(next){//支持直接写处理函数
    yield *next;
  },
  'post /':'/user/InfoController.test',
  'get /blog/(\\d{4}-\\d{2}-\\d{2})' : '/user/InfoController.test',//支持正则
  '/':'IndexController.test',//如果不写请求方式,即为all
  '/test':{
    name:'login',//给路由取名,通过router.url(name),来得到路由url,如果定义了路由文件的name属性则应在前面加上路由文件的name属性
    methods:['get','post'],//支持多种请求模式,不写则为all
    handler:'/user/InfoController.test',//表示user目录下的InfoController控制器下的test方法
  }
}

安装

$ npm install koa-router-pro --save

hander

hander是处理操作函数,可以是字符串应用,也可以直接是一个函数,也可以是一个数组,为数组的时候,将按数组顺序依次执行。

hander字符串

'post /':'/user/InfoController.test',

hander函数

'post /':function *(next){
    yield next;
},

hander数组

'post /':['IndexController.auth','IndexController.login'],//将按顺序依次执行
'post /':['IndexController.auth',function *(next){
    yield next;
}],//将按顺序依次执行

hander对象

'/test':{
  name:'login',//路由名称,可以通过router.url('login');获取路径。
  methods:['get','post'],
  handler:['IndexController.auth','IndexController.login']
},
'/test' :{
  name:'login',
  methods:['get','post'],
  handler:'IndexController.auth'
},
'/test' :{
  name:'login',
  methods:['get','post'],
  handler:function *(next){
    yield next;
  }
},

例子

routes.js 路由支持正则如:get /blog/(\d{4}-\d{2}-\d{2})

module.exports.prefix = '/user';//路由文件中的路由前缀,可选项
module.exports.name = 'user::';//路由文件的name属性,如果路由中用了name属性并假设至为login,则可通过router.url('user::login')来获得路由地址,可选项
module.exports.routes = {
  '/info':function *(next){//支持直接写处理函数
    yield *next;
  },
  'post /':'/user/InfoController.test',
  '/':'IndexController.test',//如果不写请求方式,即为all
  '/test':{
    name:'login',//给路由取名,通过router.url('user::login'),来得到路由url
    methods:['get','post'],//支持多种请求模式,不写则为all
    handler:'/user/InfoController.test',//表示user目录下的InfoController控制器下的test方法
  }
}

root目录下的IndexController.js 默认会自动把root目录下的以Controller结尾的js文件注册到路由中去,url为路径加控制器命加方法名,routes里面的路由如果跟自动路由重复,routes配置项中的路由如果跟自动路由重复,routes配置项中的路由会覆盖自动路由


module.exports = {
  test:function *(next){//路由就是/index/test
    this.body = 'test';
    yield next;
  },
};

app.js

var routes = rquire('routes.js');//可通过遍历一个目录下的所有路由文件得到一个数组然后传给routerPro的routes属性
var routerPro = require('koa-router-pro');
var router = routerPro({
  root:__dirname+path.sep+'app'+path.sep+'controllers'+path.sep,//controller的根目录
  routes:[routes],//一个数组每个数组元素是一个包含routes和prefix的对象prefix为可选项
  prefix:'/v1'//路由前缀,可选项
});
app.use(router.routes());

作者

欢迎交流!

QQ:1067302838

blog:coder.cat

githup:https://github.com/acodercat/koa-router-pro