// Event 모듈의 Event Emitter Class 사용 예시// events의 EventEmitter 클래스 가져오기const EventEmitter =require('events');// 이 객체는 on(등록) 및 emission(사용) 메서드를 사용할 수 있음const celebrity =newEventEmitter();// on은 이벤트 이름과 이벤트가 트리거 될 때, 실행할 콜백함수를 지정// Observer1의 on 등록
celebrity.on('update post',()=>{
console.log('This post is so awesome!');});// Observer2의 on 등록
celebrity.on('update post',()=>{
console.log('I like this post!');});// emit으로 해당 이름의 이벤트 트리거(발생)
celebrity.emit('update post');
emit()에 추가 argument를 통해 해당 argument를 on에서 받아 실행할 수 있음
// emit()으로 추가 argument 보내기...
celebrity.on('update post',(type)=>{
console.log(`I like this ${type} post!`);});
celebrity.emit('update post','image');
// process 모듈 사용 --> 여러 이벤트 중 beforeExit, exit 사용const process =require('node:process');
process.on('beforeExit',(code)=>{
console.log('Process beforeExit event with code: ', code);});
process.on('exit',(code)=>{
console.log('Process exit event with code: ', code);});
console.log('This message is displayed first.');// 출력// This message is displayed first.// Process beforeExit event with code: 0// Process exit event with code: 0