Docs
/
Node Express
Chapter 3

03 — Events & Streams

Core Concepts

EventEmitter

  • Node's pub/sub pattern — emit events, register listeners
  • on(event, fn) — listen for event
  • once(event, fn) — listen once then auto-remove
  • emit(event, ...args) — fire event with data
  • off(event, fn) / removeListener — remove listener

Streams

  • Readable — source of data (file read, HTTP request)
  • Writable — destination for data (file write, HTTP response)
  • Duplex — both readable and writable (TCP socket)
  • Transform — modify data as it passes through (compression, encryption)
  • pipeline() — connect streams with proper error handling

Why Streams?

  • Process data in chunks instead of loading everything into memory
  • A 1GB file needs ~1GB RAM without streams, but only ~64KB with streams