router-decorators
v1.0.0
Published
ES7 decorators that simplify Koa route creation
Downloads
2
Maintainers
Readme
router-decorators
ES7 decorators that simplify Koa route creation
本地安装
npm install router-decorators --save
使用样例
app.js
const Koa = require('koa');
const Router = require('router-decorators').router;
const app = new Koa();
const router = new Router({
app,
apiDirPath: `${__dirname}/controllers` // controllers
});
app.use(router.routes());
app.use(router.allowedMethods());
...
app.listen(8080);
/controllers/user.js
const Koa = require('koa');
const {
Controller,
Get,
Post,
Middleware,
Required
} = require('router-decorators');
@Controller('/user')
class UserController {
static async middlewareLog(ctx, next) {
console.log('This is middleware');
await next();
}
@Post('/login')
@Required({
body: {
userEmail: 'string',
password: 'string',
}
})
static async login(ctx) {
ctx.body = true;
}
@Get('/:userId')
@Required({ params: 'userId' })params
@Middleware(UserController.middlewareLog)
static async getUserInfo(ctx) {
ctx.body = { userName: 'kico' };
}
}
export default UserController;
同时也支持ts文件