koa-decorate
Provides decorators for router middleware of koa.
Install
Config
Koa-decorate is based on the decorator provided by es7, but nodejs does not support the decorator temporarily. so we need to use typescript to develop our application, we can run our typescript directly through ts-node without offline compiling.
npm install --save-dev ts-node nodemon
nodemon.json
{
"restartable": "rs",
"ignore": [
".git",
"node_modules"
],
"verbose": true,
"execMap": {
"ts": "ts-node"
},
"watch": [
"controller",
"app.ts"
],
"ext": "ts"
}
API Reference
-
-
- new Decorator([opts])
- .routes ⇒
function
-
decorator
- http-method ⇒
@Get|@Post|@Put|@Delete|@All
- path ⇒
@Path
- parameter ⇒
@Param|@Query|@Body|@Ctx|@Next
- hook ⇒
@Before|@After
- http-method ⇒
-
Decorator
Kind: Exported class <a></a>
new Decorator([opts])
Create a new decorated router.
Param | Type | Description |
---|---|---|
[opts] | Object |
|
[opts.router] | Object |
koa-router instance |
[opts.controllers] | Object |
route controller classes |
Decorator.routes ⇒ function
Returns router middleware which dispatches a route matching the request.
Kind: instance property of [Decorator](#module_koa-router--Decorator_new)
Example Basic usage:
// app.ts
import Koa from 'koa';
import Router from 'koa-router';
import Decorator from 'koa-decorate';
import Controller from './controller'; // Route controller classes
const routes = new Decorator({
router: new Router(),
controllers: Controller
}).routes();
app.use(routes);
http-method ⇒ @Get|@Post|@Put|@Delete|@All
Create @Verb
methods to match against HTTP methods, where Verb is one of the HTTP verbs
such as @Get
or @Post
etc.
Additionaly, @All
can be used to match against all methods.
Example
// CatController.ts
import { Path, Get, Post } from 'koa-decorate';
@Path('/api/cat')
class CatController {
@Get
@Path('/info')
getCatInfo () {
return {
id: 1,
name: 'Lina Weiss',
type: 'Norwegian Forest Cat'
}
}
@Post
@Path('/info/')
CreateCat () {
return {
status: 200,
data: {
id: 2
},
message: 'Created successfully...'
}
}
}
export { CatController };
path ⇒ @Path
Match URL patterns to callback functions or controller actions using @Path
,
when authFunc
returns true, controller can execute logical actions, otherwise access denied.
Param | Type | Description |
---|---|---|
path | String |
|
[authFunc] | Function => Boolean |
route callback |
Example
// CatController.ts
import { Path, Get } from 'koa-decorate';
@Path('/api/cat')
class CatController {
@Get
@Path('/info/:id', (ctx) => Number(ctx.params.id) === 1)
getCatInfo () {
return {
id: 1,
name: 'Lina Weiss',
type: 'Norwegian Forest Cat'
}
}
}
export { CatController };
parameter ⇒ @Param|@Query|@Body|@Ctx|@Next
Create @Parameter
decorators, where Parameter is one of the parameter-names such
as @Param
, @Query
, @Body
etc.
Param | Type | Description |
---|---|---|
name | String |
Example
// CatController.ts
import { Path, Get, Post, Param, Query, Body, Ctx } from 'koa-decorate';
@Path('/api/cat')
class CatController {
@Get
@Path('/info/:type')
getCatInfo (
@Param('type') type: string,
@Query('info') info: string) {
return { type, info }
}
@Post
@Path('/info/:type')
CreateCat (
@Param('type') type: string,
@Body('requestBody') requestBody: any) {
return {
status: 200,
data: Object.assign(requestBody, { type }),
message: 'Created successfully...'
}
}
}
export { CatController };
hook ⇒ @Before|@After
When the routing match is correct, the hookFunc is used to deal with
the transactions @Before
and @After
processing.
Param | Type | Description |
---|---|---|
[hookFunc] | Function |
callback hook |
Example
// CatController.ts
import { Path, Get, Param, Query, Before, After } from 'koa-decorate';
@Path('/api/cat')
class CatController {
@Get
@Path('/info/:type')
@Before((ctx, next) => {
// ...
})
@After((ctx, next) => {
// ...
})
getCatInfo (
@Param('type') type: string,
@Query('info') info: string) {
return { type, info }
}
}
export { CatController };
Controller
Kind: The dictionary of route controller classes
Example
// Controller/index.ts
import { CatController } from './cat';
export default {
Cat: CatController
};
Licences
<a>MIT</a>
- 利用Node.js+Koa框架实现前后端交互的方法
- 使用 TypeScript 构建 Koa2 项目的最佳实践
- React16+Redux+Router4+Koa+Webpack服务器端渲染(按需加载,热更新)
- 前端挑战全栈 13集原创Koa2.x免费视频奉上
- iKcamp|基于Koa2搭建Node.js实战(含视频)☞ 视图Nunjucks
- nodejs中Express与Koa2对比分析
- 可能是目前市面上比较有诚意的Koa2源码解读
- vue+koa2即时聊天,实时推送比特币价格,爬取电影网站
- iKcamp|基于Koa2搭建Node.js实战(含视频)☞ 代码分层
- 使用 nuxt+iview-admin+koa2 开发项目
原文链接:https://segmentfault.com/a/1190000016661321
本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。
转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]
本文地址:https://www.javascriptcn.com/read-42532.html
文章标题:koa-decorate