All Projects → buunguyen → route-decorators

buunguyen / route-decorators

Licence: MIT License
ES7 decorators that simplify Koa and Express route creation

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to route-decorators

Tsed
📐 Ts.ED is a Node.js and TypeScript framework on top of Express to write your application with TypeScript (or ES6). It provides a lot of decorators and guideline to make your code more readable and less error-prone.
Stars: ✭ 1,941 (+2633.8%)
Mutual labels:  koa, decorators
Koa Dec Router
An ES6 decorator + class based router, support inherit, override, priority, auto load controllers, etc.
Stars: ✭ 19 (-73.24%)
Mutual labels:  koa, decorators
tsed
📐 Ts.ED is a Node.js and TypeScript framework on top of Express to write your application with TypeScript (or ES6). It provides a lot of decorators and guideline to make your code more readable and less error-prone.
Stars: ✭ 2,350 (+3209.86%)
Mutual labels:  koa, decorators
Trafficlight
🚦 Flexible NodeJS Routing Decorators for API Routing
Stars: ✭ 69 (-2.82%)
Mutual labels:  koa, decorators
type-arango
🥑 TypeArango manages ArangoDB collections, documents, relations and routes by taking advantage of TypeScript typings.
Stars: ✭ 55 (-22.54%)
Mutual labels:  routes, decorators
datalize
Parameter, query, form data validation and filtering for NodeJS.
Stars: ✭ 55 (-22.54%)
Mutual labels:  koa
laravel-route-browser
Laravel Route Browser - A web-based route list GUI for Laravel.
Stars: ✭ 48 (-32.39%)
Mutual labels:  routes
koa-yield-breakpoint
Add breakpoints around `yield` expression especially for koa@1.
Stars: ✭ 17 (-76.06%)
Mutual labels:  koa
reducer-class
Boilerplate free class-based reducer creator. Built with TypeScript. Works with Redux and NGRX. Has integration with immer.
Stars: ✭ 25 (-64.79%)
Mutual labels:  decorators
pretty-routes
Display your Laravel routes in the console, but make it pretty. 😎
Stars: ✭ 627 (+783.1%)
Mutual labels:  routes
secondhand-deal
A campus secondhand trading system based on the vue.js + stylus + koa2 + sequelize ORM + mysql, and typescript is still learning to migrate.🍌
Stars: ✭ 21 (-70.42%)
Mutual labels:  koa
slides
No description or website provided.
Stars: ✭ 27 (-61.97%)
Mutual labels:  koa
ravel
Forge past a tangle of node.js modules. Make a cool app.
Stars: ✭ 19 (-73.24%)
Mutual labels:  koa
koaton
Koaton is a CLI tool that provides a nice starting point for full stack JavaScript Web development with Koa, Ember, and Node.js along with CaminateJS and WebSockets.
Stars: ✭ 28 (-60.56%)
Mutual labels:  koa
throwback
An asynchronous middleware pattern
Stars: ✭ 25 (-64.79%)
Mutual labels:  koa
giog
It's based on githud issues and built with Vue 2.x, vue-router & vuex with server-side rendering by koa
Stars: ✭ 14 (-80.28%)
Mutual labels:  koa
tsioc
AOP, Ioc container, Boot framework, unit testing framework , activities workflow framework.
Stars: ✭ 15 (-78.87%)
Mutual labels:  decorators
Agile-Server
A simple, fast, complete Node.js server solution, based on KOA. 简单快速的 、性能强劲的、功能齐全的 node 服务器解决方案合集,基于 KOA。
Stars: ✭ 24 (-66.2%)
Mutual labels:  koa
polog
Логирование должно быть красивым
Stars: ✭ 26 (-63.38%)
Mutual labels:  decorators
discord.ts
🤖 Create your discord bot by using TypeScript and decorators!
Stars: ✭ 315 (+343.66%)
Mutual labels:  decorators

route-decorators for Koa/Express

NPM

Build Status

ES7 decorators that simplify Koa and Express route creation. Using these decorators, you can write your controllers like below and have all the routes populated.

Koa

import {controller, get, post} from 'route-decorators'

@controller('/users', middleware1)
class UserCtrl {

  @get('/:id', middleware2, middleware3)
  async get(context, next) {}

  @post(middleware2)
  async post(context, next) {}
}

Express

import {controller, get, post} from 'route-decorators'

@controller('/users', middleware1)
class UserCtrl {

  @get('/:id', middleware2, middleware3)
  async get(req, res, next) {}

  @post(middleware2)
  async post(req, res, next) {}
}

Once the decorators are applied, every controller instance will receive a $routes array, which you can use to define actual Koa/Express routes.

Assume the above UserCtrl definition, you can define routes in UserCtrl's constructor (although really you can put the code anywhere) as follows:

Koa

import Router from 'koa-66'

// Inside controller constructor
this.router = new Router()
for (const {method, url, middleware, fnName} of this.$routes) {
  this.router[method](url, ...middleware, this[fnName].bind(this))
}

Express

import express from 'express'

// Inside controller constructor
this.router = express.Router()
for (const {method, url, middleware, fnName} of this.$routes) {
  this.router[method](url, ...middleware, (req, res, next) => {
    this[fnName](req, res, next).catch(next)
  })
}

You can move the above logic to some base controller in your app and reuse it for every controller. For example:

class BaseCtrl {
  constructor() {
    this.router = new Router()
    for (const {method, url, middleware, fnName} of this.$routes) {
      this.router[method](url, ...middleware, this[fnName].bind(this))
    }
  }
}

@controller(...)
class UserCtrl extends BaseCtrl {
  // decorated methods as above
}

Decorators

  • @controller(path: optional, ...middleware: optional)
  • @route(method, path: optional, ...middleware: optional)
  • @head, @options, @get, @post, @put, @patch, @del, @delete, @all: wrappers of @route that automatically supply the method argument.

Test

npm install
npm test
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].