
Core idea: Node's built-in modules (http, fs, timers) run on an event-driven model with two roles:
π€ Emitter β an object that fires a named event when something happens (a request comes in, a timer ends, a file finishes reading)
π Listener β a callback you attach ahead of time, which runs when that event fires
π Example: HTTP server as an emitter
const server = http.createServer();
server.on('request', (req, res) => {
console.log('Request received');
res.end('Request received');
});Walking through it:
π
server.on('request', callback)registers a listener for'request'.π€ Every incoming request makes the server emit a
'request'event.β Because a listener is already attached, the callback runs and responds to the client.

Why this works: π‘ server is an instance of Node's EventEmitter class β it inherits emitting and listening behavior from there.
π This is the Observer Pattern
EventEmitter is Node's implementation of the Observer Pattern:
π The observer (listener) waits.
π€ The subject (emitter) eventually fires the event it's waiting for.
β‘ Code reacts to events instead of being called directly β the reverse of a normal function call.
π― Why it's useful
π Decoupling β modules don't call each other's functions directly.
fsdoesn't need to know anything abouthttp; each module just emits its own events, and any other code can listen and react.π₯ One event, many reactions β attach several listeners to the same event with no extra wiring.
β Takeaway
π€ Emitter fires an event β π listener reacts with a callback. π This pattern is foundational across Node's core API, and the next lecture puts it into practice directly with EventEmitter.