Socket
Socket
Sign inDemoInstall

koa-router-class

Package Overview
Dependencies
Maintainers
9
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

koa-router-class

koa 路由,将路由转发到 class 方法上 基于 path-to-regexp 库


Version published
Weekly downloads
0
decreased by-100%
Maintainers
9
Weekly downloads
 
Created
Source

koa-router-class

koa 路由,将路由转发到 class 方法上
基于 path-to-regexp 库

usage

const { Router } = require('koa-router-class');

app.use(Router());
IndexController.ts
import Result from '../../src/core/Result';
import { Path, RequestMethod, BaseController, Param, Around, middlewareToAround, Inject } from '../../src/index';
import Test from '../service/Test';

// Path 修饰 class 时,参数为根路由(参数只能一个)
// Path 修饰 method 时,参数为方法路由(参数可有多个)
@Around(async (point) => {
    console.log('----around before----')

    const { proceed, args } = point;
    const result = await proceed(args);

    console.log('----around after----')

    return result;
})
export default class Index extends BaseController {

    @Inject(Test)
    t: Test;

    @Path('/')
    index() {
        console.log(this.t.test());

        return this.send('This is index');
    }

    @Path({ value: '/post', method: RequestMethod.POST })
    post() {
        return this.send('This is post page.');
    }

    @Path('/test/:name')
    @Around(middlewareToAround((ctx, next) => {
        console.log('》》', ctx.request.path);
        return next();
    }))
    test(@Param('name') name: string) {
        console.log(`进入方法,参数:${JSON.stringify(name)}`)
        return Result.send(`这里是测试页面,地址 ${this.req.path}`);
    }

    // 私有化方法,无路由不会进入此 action
    hehe() {
        return this.send('hehe');
    }
}

FAQs

Package last updated on 05 Dec 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc