@route decorator for koa-router
install by:
npm i koa-decorator
then use in code:
import {HttpMethod, route} from 'koa-decorator';
@route('/monitor')
export default class MonitorCtrl{
@route('/alive', HttpMethod.GET)
async alive(ctx) {
ctx.body = {
data: true,
};
}
}
register to router:
import {load} from 'koa-decorator';
const apiRouter = load(path.resolve(__dirname, 'route'), '.route.js');
app.use(apiRouter.routes()).use(apiRouter.allowedMethods());
import {HttpMethod, route} from 'koa-decorator';
function auth(ctx) {
if(!ctx.auth){
ctx.response.status = 401;
}
}
@route('/monitor')
export default class MonitorCtrl {
@route('/alive', HttpMethod.GET,auth)
async alive(ctx) {
ctx.body = {
data: true,
};
}
}
The following code has the same effect as above:
@route('/monitor')
export default class MonitorCtrl {
@route('/alive', HttpMethod.GET,auth)
async alive() {
return {
data: true,
};
}
}