在使用koa 开发网站的过程中期望使用decorator ,进行日志记录
通过babel-register +
安装
npm install --save babel-plugin-transform-decorators-legacy
.babelrc
{
"plugins": ["transform-decorators-legacy"]
}
入口文件引入babel-register
const Koa = require('koa');
const app = new Koa();
require("babel-register");
var decoratorTest = require("./decoratorT.js")
app.use(async ctx => {
var a = new decoratorTest();
ctx.body = a.say();
});
app.listen(3000);
decoratorT代码,通过decorator进行日志记录
function log(className, propName, descriptor) {
var value = descriptor.value;
descriptor.value = function()
{
console.log('start');
var result = value();
console.log("end")
return result;
}
}
class A {
@log
say() {
return "wsknt"
}
}
module.exports = A
参考文档http://babeljs.io/docs/plugins/transform-decorators/#via-node-api
https://github.com/loganfsmyth/babel-plugin-transform-decorators-legacy