How to track method calls and implement another method after that? (Nodejs)

130 Views Asked by At

In my nodejs application, I have one particular method that I want to call after execution of every method. And I don't want any code modification in methods that is being called.

I have implemented few logic to solve this problem, one of them is event based implementation,

  emitter.on("after", function(data){
    console.log("----after------", data);
    let req = {
        eventType: "user-event",
        appName: "Demo-app",
        payload: "function result: "+JSON.stringify(data),
    }
    producer.publishEvent(req);
   });

   userDetails = () => {
     let data = {"name": "xyz"};
     emitter.emit("after",data);
     return data;
   }

Also I got to know after searching that I can use AOP in javascript for this purpose. But it is not fully supported in ES6 and also as of now decorators will work with plugin only in ES6.(please correct me if I am wrong here)

So is there any other way I can do this implementation without modifying my methods?

0

There are 0 best solutions below