Node.js 이벤트 모듈

❮ 내장 모듈


예시

"scream"이라는 이벤트에 대한 이벤트 리스너를 만든 다음 이벤트를 유발합니다.

var events = require('events');
var eventEmitter = new events.EventEmitter();

eventEmitter.on('scream', function() {
console.log('A scream is detected!');
});
eventEmitter.emit('scream');

정의 및 사용

이벤트 모듈은 이벤트 작업 방법을 제공합니다.

Node.js에서 모든 이벤트는 EventEmitter 객체의 인스턴스입니다.


통사론

이벤트 모듈을 포함하고 애플리케이션에서 EventEmitter를 생성하기 위한 구문:

var events = require('events');
var eventEmitter = new events.EventEmitter();

EventEmitter 속성 및 메서드

Method Description
addListener() Adds the specified listener
defaultMaxListeners Sets the maximum number of listeners allowed for one event. Default is 10
emit() Call all the listeners registered with the specified name
eventNames() Returns an array containing all registered events
getMaxListeners() Returns the maximum number of listeners allowed for one event
listenerCount() Returns the number of listeners with the specified name
listeners() Returns an array of listeners with the specified name
on() Adds the specified listener
once() Adds the specified listener once. When the specified listener has been executed, the listener is removed
prependListener() Adds the specified listener as the first event with the specified name
prependOnceListener() Adds the specified listener as the first event with the specified name, once. When the specified listener has been executed, the listener is removed
removeAllListeners() Removes all listeners with the specified name, or ALL listeners if no name is specified
removeListener() Removes the specified listener with the specified name
setMaxListeners() Sets the maximum number of listeners allowed for one event. Default is 10

❮ 내장 모듈