Skip to content

shibin-cli/decorator-router

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

koa-decorator-router

npm

中文文档

Decorator Routing

Install

npm i koa-decorator-router -S
import Route, {
  post,
  put,
  del,
  get,
  controller,
  convert
} from 'koa-decorator-router'
import Koa from 'koa'
import Router from 'koa-router'

const app = new Koa()
const router = new Router()
const route = new Route()

const middleware1 = convert(async (ctx, next) => {
  console.log("middleware1")
  await next()
})

@controller('/article')
class Article {

  @get('/detail/:id')
  @middleware1
  getDetail(ctx) {
    ctx.body = `detail ${ctx.params.id}`
  }

  @put('/add')
  addArticle(ctx) {
    ctx.body = 'add'
  }

  @post('/post')
  updateArticle(ctx) {
    ctx.body = 'post'
  }

  @del('/del')
  delArticle(ctx) {
    ctx.body = 'del'
  }
}

route.init(router)
app
  .use(router.routes())
  .use(router.allowedMethods())
app.listen(3000)

Api

  • controller

methods

  • get
  • put
  • post
  • all
  • convert
  • required

Route

  • setRouterPath Set the location of the import routing folder and import files
  • init Functions for mounting routing
  • setRouterPathAndInit Import routing file and automatically initialize after completion
route.setRouterPathAndInit(path.resolve(__dirname, './routes'), router)
app
  .use(router.routes())
  .use(router.allowedMethods())
app.listen(3000)

or

async function start(){
  await route.setRouterPath(path.resolve(__dirname, './routes'))
  route.init(router)
  app
    .use(router.routes())
    .use(router.allowedMethods())
  app.listen(3000)
}
start()

About

Decorator Routing

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published