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
andquery
base onfastest-validator
- Log request in and out
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` 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
.
import KoaAutoboot, { Controller, Post, Route, Middleware, Body } from 'koa-autoboot'
KoaAutoboot(
// The options
{
// string, the folder of controller files
dir: __dirname,
// string (optional), the prefix append to all routes path
prefix: 'api',
// (string | RegExp)[] | ((s: string) => boolean), which files should be ignore in dir.
ignore: [],
// 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
}
/**
* 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`
*/
@Controller('/')
// Add koa middlewares to the router.
@Middleware([cors()])
export default class IndexController {
// Add koa middlewares to a route.
@Middleware([cors()])
// Add a `POST` route, pass the path (optional), default is the method name.
@Post()
// Add a route, pass the http methods, default is `['GET', 'POST']`.
@Route(['PUT'])
// Inject the `ctx.request.body` as the argument, pass the validate schema (optional)
@Body({ name: 'string' })
// Inject the `ctx.request.query` as the argument
@Query()
// Inject the `ctx` as the argument
@Ctx()
async greeting(ctx: Koa.Context, query: any, body: GreetingParams): Promise<string> {
// Use return value as the response
return `Hello, ${body.name}`
}
}
npm install
npm start
Will run the example koa server with nodemon.