Coder Social home page Coder Social logo

koa-route-decors's Introduction

koa-route-decors

Install

  npm install koa-route-decors

Developing

  • dependency injection (finished)

Update

  • 1.05 fix compilation of ts files into js files that could not load routing properly

  • 1.03 add dependency injection

  • 1.02 fix 'this' in routing method

Api

decorators

  • @Controller
  • @Get
  • @Post
  • @Put
  • @Delete
  • @Options
  • @Patch
  • @Injectable

init Router Function

  • initRouter
  • autoRouter

Example

huzz-koa-template

Usage

app.ts

// Manual router
import * as Koa from 'koa';
import {initRouter} from 'koa-route-decors';
import {User} from './controller/user';

const app = new Koa();

const router = new Router();

const subRouter = initRouter(User);

router.use(subRouter);
// ...

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

app.listen(8080);


// Auto router

// ...

import {autoRouter} from 'koa-route-decors';

start();

async function start() {
  const app = new Koa();
  const router = new Router();
  const subRouter = await autoRouter(controllerPath); // recursive all *.controller.ts in Given path example: path.resolve(__dirname, './');
  app.use(router.routes()).use(router.allowedMethods());
  app.listen(8080);
}


user.controller.ts

/**
 * @decorator
 * @params {string} path - route prefix
*/
@Controller('/user')
export class User {

  constructor(private userService: UserService) {}
  /**
   * @decorator
   * @params {string} path - route path, default method name 'findUser'
  */
  @Get('/findUser')
  async findUser(ctx: Context, next: Function) {  // final route path '/user/findUser'
    this.userService.findOne();
    // ...
  }

  @Post()
  async addUser() {  // '/user/addUser'
    // ...
  }
}

user.service.ts

@Injectable()
export class UserService {
  // ...
  findOne() {
    // pass
  }
}

koa-route-decors's People

Contributors

xhuz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.