Coder Social home page Coder Social logo

notbucai / ts-koa-router-decorator Goto Github PK

View Code? Open in Web Editor NEW
13.0 3.0 1.0 86 KB

🤩🥳使用 TypeScript 的装饰器 配合 Koa2 和 koa-router 实现类似于 Spring boot 注解

JavaScript 6.62% TypeScript 93.38%
typescript spring-boot decorator koa koa-router

ts-koa-router-decorator's Introduction

typescript 装饰器实现 koa 路由 (工程模板)

使用 TypeScript 的装饰器 实现类似于 Spring boot 注解
用于方便的编写路由
如在使用过程中碰到问题可提交 issues 或者 通过Email: [email protected]

原理

  1. 首先需要知道 ts 装饰器的执行顺序
  2. 然后一步一步的对相关的装饰器进行反射
  3. 然后动态加载模块
  4. 解析相关的模块
  5. 将模块中的方法进行解析
  6. 将解析的方法 打包成一个路由函数
  7. 将函数加载到koa-router

实现的‘注解’

名称 用法 说明 参数必要
Controller @Controller('\user') 用于class的注解
GET @GET(':id') 创建一个GET方式的路由。用于方法的注解
POST @POST(':id') 创建一个GET方式的路由。用于方法的注解
RequestQuery @RequestQuery('id') 将query的指定属性绑定到当前的参数。用于函数的参数的注解
RequestBody @RequestBody 将body的指定属性绑定到当前的参数。用于函数的参数的注解
RequestParam @RequestParam('id') 将param的指定属性绑定到当前的参数。用于函数的参数的注解

使用

初始化

// 1. 将本仓库clone下来  
$ git clone https://github.com/wuxinweb/ts-koa-router-decorator.git
// 2. 安装依赖
$ npm install 
// or
$ yarn
// 3. 启动
$ yarn dev 
// or 
$ npm run dev

编码规范

src/controller 目录为控制器目录 统一编写后会异步加载到路由

每个控制器都需要创建一个文件且须放在 src/controller 目录下 可建二级目录

示例

文件: src/controller/User.ts 中可作为模板

ts-koa-router-decorator's People

Contributors

notbucai avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

yi1920

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.