资讯专栏INFORMATION COLUMN

koa-decorate

goji / 2270人阅读

koa-decorate




</>复制代码

  1. Provides decorators for router middleware of koa.
Install

Config

</>复制代码

  1. 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.

</>复制代码

  1. npm install --save-dev ts-node nodemon

nodemon.json

</>复制代码

  1. {
  2. "restartable": "rs",
  3. "ignore": [
  4. ".git",
  5. "node_modules"
  6. ],
  7. "verbose": true,
  8. "execMap": {
  9. "ts": "ts-node"
  10. },
  11. "watch": [
  12. "controller",
  13. "app.ts"
  14. ],
  15. "ext": "ts"
  16. }
API Reference

koa-decorate

Decorator ⏏

[new Decorator([opts])](#module_koa-decorate--Decorator_new)

.routes ⇒ function

decorator

http-method ⇒ @Get|@Post|@Put|@Delete|@All

path ⇒ @Path

parameter ⇒ @Param|@Query|@Body|@Ctx|@Next

hook ⇒ @Before|@After

Controller

Decorator

Kind: Exported class

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

Example
Basic usage:

</>复制代码

  1. // app.ts
  2. import Koa from "koa";
  3. import Router from "koa-router";
  4. import Decorator from "koa-decorate";
  5. import Controller from "./controller"; // Route controller classes
  6. const routes = new Decorator({
  7. router: new Router(),
  8. controllers: Controller
  9. }).routes();
  10. 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

</>复制代码

  1. // CatController.ts
  2. import { Path, Get, Post } from "koa-decorate";
  3. @Path("/api/cat")
  4. class CatController {
  5. @Get
  6. @Path("/info")
  7. getCatInfo () {
  8. return {
  9. id: 1,
  10. name: "Lina Weiss",
  11. type: "Norwegian Forest Cat"
  12. }
  13. }
  14. @Post
  15. @Path("/info/")
  16. CreateCat () {
  17. return {
  18. status: 200,
  19. data: {
  20. id: 2
  21. },
  22. message: "Created successfully..."
  23. }
  24. }
  25. }
  26. 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

</>复制代码

  1. // CatController.ts
  2. import { Path, Get } from "koa-decorate";
  3. @Path("/api/cat")
  4. class CatController {
  5. @Get
  6. @Path("/info/:id", (ctx) => Number(ctx.params.id) === 1)
  7. getCatInfo () {
  8. return {
  9. id: 1,
  10. name: "Lina Weiss",
  11. type: "Norwegian Forest Cat"
  12. }
  13. }
  14. }
  15. 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

</>复制代码

  1. // CatController.ts
  2. import { Path, Get, Post, Param, Query, Body, Ctx } from "koa-decorate";
  3. @Path("/api/cat")
  4. class CatController {
  5. @Get
  6. @Path("/info/:type")
  7. getCatInfo (
  8. @Param("type") type: string,
  9. @Query("info") info: string) {
  10. return { type, info }
  11. }
  12. @Post
  13. @Path("/info/:type")
  14. CreateCat (
  15. @Param("type") type: string,
  16. @Body("requestBody") requestBody: any) {
  17. return {
  18. status: 200,
  19. data: Object.assign(requestBody, { type }),
  20. message: "Created successfully..."
  21. }
  22. }
  23. }
  24. 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

</>复制代码

  1. // CatController.ts
  2. import { Path, Get, Param, Query, Before, After } from "koa-decorate";
  3. @Path("/api/cat")
  4. class CatController {
  5. @Get
  6. @Path("/info/:type")
  7. @Before((ctx, next) => {
  8. // ...
  9. })
  10. @After((ctx, next) => {
  11. // ...
  12. })
  13. getCatInfo (
  14. @Param("type") type: string,
  15. @Query("info") info: string) {
  16. return { type, info }
  17. }
  18. }
  19. export { CatController };

Controller

Kind: The dictionary of route controller classes

Example

</>复制代码

  1. // Controller/index.ts
  2. import { CatController } from "./cat";
  3. export default {
  4. Cat: CatController
  5. };
Licences

MIT

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/98317.html

相关文章

发表评论

0条评论

goji

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<