GithubHelp home page GithubHelp logo

koa-autoboot's Introduction

Koa Autoboot

Auto bootstrap routes from a controller folder.

  • Auto build routes from controller files
  • Use the return value as response body
  • Inject ctx, body, query or custom data
  • Build-in validator for body and query base on fastest-validator
  • Log request in and out

Usage

Install

npm install koa-autoboot koa-body

Basic usage

// controllers/IndexController.ts
import Koa from 'koa'
import { Controller, Get, Ctx } from 'koa-autoboot'

@Controller('/')
export default class IndexController {
  @Get()
  @Ctx() // Inject the `ctx.request.body` as the argument.
  async greeting(ctx: Koa.Context) {
    ctx.body = 'Hello world'
  }
}
// index.ts
import path from 'path'
import Koa from 'koa'

const app = new Koa()
app.use(
  KoaAutoboot({
    dir: path.join(__dirname, 'controllers'),
  }),
)
app.listen(4000)

curl http://localhost:4000/greeting will get Hello world.

APIs

import KoaAutoboot, { Controller, Post, Route, Middleware, Body } from 'koa-autoboot'

KoaAutoboot({
  dir: __dirname   // string, the folder of controller files
  prefix: 'api'    // string (optional), the prefix append to all routes path
  // function (optional), parse return value to response body.
  // If return value is `undefined`, parser will not be call.
  // Default parser:
  returnParser: (value: any) => {
    const body = { status: true, message: 'Success', data: null }

    if (value === false || value instanceof Error) {
      body.status = false
      body.message = value.message || 'Fail'
    } else {
      body.data = value
    }

    return body
  }
})

interface GreetingParams {
  name: string
}

@Controller('/')
/**
 * Mark the class is a router.
 * Pass a router path (optional), the default values is className.toLowerCase().replace('controller', '')
 * For this class, the default router path is `index`
 */
@Middleware([cors()])
/**
 * Add koa middlewares to the router.
 */
export default class IndexController {
  @Middleware([cors()])   // Add koa middlewares to a route.
  @Post()                 // Add a `POST` route, pass the path (optional), default is the method name.
  @Route(['PUT'])         // Add a route, pass the http methods, default is `['GET', 'POST']`.
  @Body()                 // Inject the `ctx.request.body` as the argument, pass the validate schema (optional)
  async greeting(params: GreetingParams): Promise<string> {
    // Use return value to response feature
    return `Hello, ${params.name}`
  }
}

Yes, this is all apis.

Development

npm install
npm start

Will run the example koa server with nodemon.

koa-autoboot's People

Contributors

4074 avatar

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.