0.0.3 • Published 7 years ago

koa-router-decoration v0.0.3

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

koa-router-decoration

Usage

npm install koa-router-decoration --save
npm install koa-validator-middleware --save

UserController.js

import RouterDecorator, {
    RequestMapping, GetMapping, PostMapping, DeleteMapping} from 'koa-router-decoration';
import {NotEmpty} from 'koa-validator-middleware';

@RequestMapping('/users')
export default class UserController extends RouterDecorator {
    constructor() {
        super();
    }
    
    @GetMapping('/')
    async getUsers(ctx) {
        
    }
    
    @GetMapping('/:userId')
    async getUser(ctx) {
        const userId = ctx.params.userId;
    }
    
    @PostMapping('/', NotEmpty('request.body.username'))
    async createUser(ctx) {
        if (!ctx.validation.pass) {
            //@see https://github.com/miaowing/koa-validator-middleware for detail.
        }
    }
    
    @DeleteMapping('/:userId')
    async deleteUser(ctx) {
        
    }
}

app.js

import Koa from 'koa';
import KoaRouter from 'koa-router';

import UserController from './UserController';

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

rootRouter.use(new UserController());

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

app.start(3000);

API

Route(path, method, ...middleware)

RequestMapping(path, method, ...middleware)

The same as @Route.

GetMapping(path, ...middleware)

PostMapping(path, ...middleware)

PutMapping(path, ...middleware)

DeleteMapping(path, ...middleware)