Node.js has an event-driven architecture. Most of Node.js' core API is built around EventEmitter. This is a Node.js module that allows listeners to subscribe to certain named events that can be triggered later by an emitter.
You can define your own event emitter easily by just including the events Node.js module and creating a new instance of EventEmitter:
const EventEmitter = require('events') const emitter = new EventEmitter() emitter.on('welcome', () => { console.log('Welcome!') })
Then, you can trigger the welcome event by using the emit method:
emitter.emit('welcome')
It is actually, pretty simple. One of the advantages is that you can subscribe multiple listeners to the same event, and they will get triggered when the emit method is used:
emitter.on('welcome', () => { ...