在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:gwuhaolin/koa-router-decorator开源软件地址:https://github.com/gwuhaolin/koa-router-decorator开源编程语言:TypeScript 100.0%开源软件介绍:koa-decorator@route decorator for koa-router Useinstall 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'));
// const apiRouter = load(path.resolve(__dirname, 'route', '.ts')); // only require .ts files as route
app.use(apiRouter.routes()).use(apiRouter.allowedMethods()); Use auth middlewareimport {HttpMethod, route} from 'koa-decorator';
function auth(ctx) {
if(!ctx.auth){
ctx.response.status = 401;
}
}
@route('/monitor')
export default class MonitorCtrl {
@route('/alive', HttpMethod.ALL, auth)
async alive(ctx) {
ctx.body = {
data: true,
};
}
} Return data directThe following code has the same effect as above: @route('/monitor')
export default class MonitorCtrl {
@route('/alive', HttpMethod.GET, auth)
async alive() {
return {
data: true,
};
}
} |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论