我想在我的LoopBack4应用程序中增强一个Interceptor,该应用程序目前仅在命令行上向我显示控制器方法调用的开始和结束-如此处所述:https://loopback.io/doc/en/lb4/Interceptors.html
我的日志拦截器看起来像这样:
export const Log: Interceptor = async (invocationCtx, next) => {
// Wait until the interceptor/method chain returns
const req = await invocationCtx.get(RestBindings.Http.REQUEST);
try
{
const stackinfo = 'Class: ' + invocationCtx.targetClass.name + ' | Method: ' + invocationCtx.methodName + " | Request IPs: " + req.ips.concat(', ');
logger.trace('Starting - ' + stackinfo);
const result = await next();
const res = await invocationCtx.get(RestBindings.Http.RESPONSE);
logger.trace('Ending - ' + stackinfo + ' | Response Status Code: ' + res.statusCode);
return result;
}
catch (e)
{
logger.error(e);
throw e;
}
};
现在,我想增强此拦截器,以便还将一些统计数据记录到我的MySQL-Datasource中。我的问题是,如何访问拦截器中的存储库?我是否必须注入存储库?如果是,我该怎么做?还是有更好的方法来实现这一目标?
我自己找到了一个解决方案: