Listen to All Emitted Events in Node.js

后端 未结 12 1323
孤街浪徒
孤街浪徒 2020-12-24 00:06

In Node.js is there any way to listen to all events emitted by an EventEmitter object?

e.g., can you do something like...

event_emitter.on(\         


        
12条回答
  •  猫巷女王i
    2020-12-24 00:37

    As mentioned this behavior is not in node.js core. But you can use hij1nx's EventEmitter2:

    https://github.com/hij1nx/EventEmitter2

    It won't break any existing code using EventEmitter, but adds support for namespaces and wildcards. For example:

    server.on('foo.*', function(value1, value2) {
      console.log(this.event, value1, value2);
    });
    

提交回复
热议问题