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 🙏

© 2026 – Pkg Stats / Ryan Hefner

koa-router-decorator-plugin

v0.0.5

Published

koa router decorator

Downloads

29

Readme

koa-router-decorator-plugin

一个 koa router 装饰器

安装

npm i koa-router-decorator-plugin --save

使用

controller/example01.ts 文件配置如下:

import { Controller, RequestMapping } from "koa-router-decorator-plugin";

@Controller
export default class {
  @RequestMapping({ method: "GET", url: "/example01" })
  async example01(ctx) {
    ctx.body = "example01";
  }
}

或者, 添加 koa-router 通用配置,

controller/example02.ts 文件配置如下:

import { Controller, RequestMapping } from "koa-router-decorator-plugin";

@Controller({ prefix: "/prefix" })
export default class {
  @RequestMapping({ method: "GET", url: "/example02" })
  async example01(ctx, _) {
    ctx.body = "example02";
  }

  @RequestMapping({ method: "GET", url: ["/example03", "/example04"] })
  async example02(ctx, _) {
    ctx.body = "example03";
  }
}

koakoa-router 中使用,

import * as Koa from "koa";
import * as KoaRouter from "koa-router";
import * as path from "path";
import KoaDecoratorRouter, {
  loadDecoratorRouter
} from "koa-router-decorator-plugin";

const app = new Koa();
const router = new KoaRouter();

// 1.加载配置 controller 文件
loadDecoratorRouter({
  dir: path.join(__dirname, "/path/to/controller"),
  extension: ".ts"
});

router.get("/heartbeats", async (ctx, _) => {
  ctx.body = "success";
});

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

// 2.app.use 使用已配置的路由
app.use(KoaDecoratorRouter.routes()).use(KoaDecoratorRouter.allowedMethods());

app.listen(4000);

输出结果:

http.get("http://localhost:4000/heartbeats"); // output: success

http.get("http://localhost:4000/example01"); // output: example01

http.get("http://localhost:4000/prefix/example02"); // output: example02

http.get("http://localhost:4000/prefix/example03"); // output: example03

http.get("http://localhost:4000/prefix/example04"); // output: example04

API

@Controller

参数:支持传入 koa-router Router instance 中所有的方法。不传参,可以直接调用 @Controller 来使用。

router.prefix, 可以这么配置 @Controller({prefix: '/user'});

Router instance 方法中多参数调用,需要以数组形式参入。

例如,

router.url("user", { id: 3 }, { query: { limit: 1 } });

可以这么配置,

@Controller({url: ['user', { id: 3 }, {query: { limit: 1 }}]});

@RequestMapping({method: string, url: string | string[], middleware?: []})

method

koa router HTTP METHOD 值,支持大小写

url

koa router 中请求 url 配置,可传递 url 数组

middleware

请求中间件配置,

@RequestMapping({
  method: 'GET',
  url: '/users/:id',
  middleware: [
   (ctx, next) => {
     return User.findOne(ctx.params.id).then(function(user) {
       ctx.user = user;
       next();
     });
   },
   // ...middleware
  ]
})
ctx => {
 console.log(ctx.user);
}

转化为 koa-router 配置,

router.get(
  "/users/:id",
  (ctx, next) => {
    return User.findOne(ctx.params.id).then(function(user) {
      ctx.user = user;
      next();
    });
  },
  // ...middleware
  ctx => {
    console.log(ctx.user);
  }
);

loadDecoratorRouter(dir: string, extension?: string)

该方法调用之后,将写入 koa-router-decorator-plugin 里的全局变量, koa 使用的时候,直接从 koa-router-decorator-plugin 导出这个全局变量 KoaDecoratorRouter 使用即可。

dir

使用 koa-router-decorator-plugin 配置文件目录路径

extension(default = '.js')

匹配配置文件的拓展名

不使用 loadDecoratorRouter 全局引用,也可以单个文件引用,示例如下:

import "path/to/controller.ts";
import KoaDecoratorRouter from "koa-router-decorator-plugin";

app.use(KoaDecoratorRouter.routes()).use(KoaDecoratorRouter.allowedMethods());